【问题标题】:How do I get a SecurityTokenHandler to load its configuration from web.config?如何让 SecurityTokenHandler 从 web.config 加载其配置?
【发布时间】:2018-07-24 07:05:18
【问题描述】:

我一直在努力尝试使用 WIF 将 SAML2 SSO 入口点添加到 asp.net 4.6 Web 应用程序 - 这是我在开始之前完全不熟悉的技术。到目前为止的工作是以编程方式创建所有内容,为此我对各种对象类型进行了子类化,例如 Saml2SecurityTokenHandler 和 X509CertificateValidator 和 IssuerNameRegistry,对于这个处理程序,我从头开始构建一个 SecurityTokenHandlerConfiguration 对象。但我注意到,正确的做法是从 web.config 加载 SecurityTokenHandlerConfiguration,或者更确切地说是 app.config,因为这是在侧面程序集中而不是网站本身。

如果我能让它发挥作用,我可以删除很多我一直在整理的程序化内容。所以我开始将必要的部分放在 web.config 中。我将 identityModel 部分添加到 configSections 标记中,并在我的配置中添加了类似的内容:

<system.identityModel>
  <identityConfiguration>
    <tokenReplayDetection enabled="true" />
    <audienceUris>
      <add value="http://myurl.com" />
    </audienceUris>
    <issuerNameRegistry type="System.IdentityModel.Tokens.ConfigurationBasedIssuerNameRegistry, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089">
      <trustedIssuers>
        <add thumbprint="1234123412341234ABCDABCDABCDABCD00000001" name="theirurl.com" />
      </trustedIssuers>
    </issuerNameRegistry>
  </identityConfiguration>
</system.identityModel>

我也尝试过这样设置,看起来它应该适合我的需要:

<system.identityModel>
  <identityConfiguration>
    <securityTokenHandlers>
      <securityTokenHandlerConfiguration>
        <tokenReplayDetection enabled="true" />
        <audienceUris mode="Always">
          <add value="http://myurl.com" />
        </audienceUris>
        <issuerNameRegistry type="System.IdentityModel.Tokens.ConfigurationBasedIssuerNameRegistry, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089">
          <trustedIssuers>
            <add thumbprint="1234123412341234ABCDABCDABCDABCD00000001" name="theirurl.com" />
          </trustedIssuers>
        </issuerNameRegistry>
      </securityTokenHandlerConfiguration>
    </securityTokenHandlers>
  </identityConfiguration>
</system.identityModel>

问题是我似乎无法弥合配置和代码之间的差距。似乎没有什么可以自动加载它,而且我找不到任何关于如何手动加载它的有用信息。似乎整个部分都被忽略了。如果需要“加载配置”步骤,我找不到它的描述。

如何构造 Saml2SecurityTokenHandler 的实例,并从 app.config 中的内容加载其配置?

更新

我不再追求这种方法。我仍然有点想知道这是如何工作的,但它不再重要了。

【问题讨论】:

    标签: asp.net saml wif


    【解决方案1】:

    通过创建IdentityConfiguration 的实例,它将从配置中读取您的设置。默认构造函数将执行此操作,但还有一个构造函数重载可让您显式指定此操作 (new IdentityConfiguration(true))。如果没有 配置元素,那也会给你一个例外。

    如果您尚未清除 SecurityTokenHandlers 的集合,您将能够通过 IdentityConfiguration 实例的 SecurityTokenHandlers 属性访问其中的各种。这对我来说是正确的,所以我必须搜索我正在寻找的处理程序。

    就我而言,我想读取 SessionSecurityTokenHandler 的 TokenLifeTime 属性(另外使用 System.Linq):

    System.IdentityModel.Tokens.SessionSecurityTokenHandler sessionSecurityTokenHandler =
        new IdentityConfiguration(true)
        .SecurityTokenHandlers
        .SingleOrDefault(sth => sth.TokenType == typeof(System.IdentityModel.Tokens.SessionSecurityToken))
        as System.IdentityModel.Tokens.SessionSecurityTokenHandler;
    
    TimeSpan tokenLifeTime = sessionSecurityTokenHandler.TokenLifetime;
    

    我的配置如下:

    <system.identityModel>
        <identityConfiguration>
            <securityTokenHandlers>
                <remove type="System.IdentityModel.Tokens.SessionSecurityTokenHandler, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
                <add type="System.IdentityModel.Tokens.SessionSecurityTokenHandler, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
                    <sessionTokenRequirement lifetime="01:00"  />
                </add>
            </securityTokenHandlers>
        </identityConfiguration>
    </system.identityModel>
    

    来自securityTokenHandlers documentation

    默认情况下,集合填充有以下处理程序类型:SamlSecurityTokenHandler、Saml2SecurityTokenHandler、KerberosSecurityTokenHandler、WindowsUserNameSecurityTokenHandler、RsaSecurityTokenHandler、X509SecurityTokenHandler 和 EncryptedSecurityTokenHandler。您可以使用添加、删除和清除元素来修改集合。您必须确保集合中仅存在任何特定类型的单个处理程序。例如,如果您从 Saml2SecurityTokenHandler 类派生处理程序,则您的处理程序或 Saml2SecurityTokenHandler 可以配置在单个集合中,但不能同时配置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-01
      • 1970-01-01
      • 2021-01-09
      • 1970-01-01
      • 1970-01-01
      • 2013-05-31
      • 2013-10-14
      • 1970-01-01
      相关资源
      最近更新 更多