【问题标题】:How to set the NameClaimType in an ASP.Net MVC 5 site?如何在 ASP.Net MVC 5 站点中设置 NameClaimType?
【发布时间】:2013-12-05 01:48:13
【问题描述】:

我使用 Microsoft 的“本地”组织帐户身份验证机制创建了一个 ASP.Net MVC 5 站点。这最终配置为指向我公司的 ADFS 基础架构。我正在取回所有已配置的声明。但是,在运行时,ClaimsIdentity.Name 是空白的。这是因为默认情况下,ClaimsIdentity.NameClaimType 似乎是:

http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name

但是,我希望将 ClaimsIdentity.Name 映射到:

http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier 

根据Microsoft Docs,在 web.config 中设置这个的地方是在 securityTokenHandlers 元素的 Add 元素中:

<system.identityModel>
  <identityConfiguration>
    <securityTokenHandlers>
      <add>
        <samlSecurityTokenRequirement>
          <nameClaimType value=xs:string>
          </nameClaimType>
        </samlSecurityTokenRequirement>
      </add>
    </securityTokenHandlers>
  </identityConfiguration>
</system.identityModel>

在我的 ASP.Net MVC 5 web.config 中,唯一看起来适用并通过智能感知检查的东西最终看起来像这样:

<system.identityModel>
  <identityConfiguration>
    <securityTokenHandlers>
      <add type="System.IdentityModel.Services.Tokens.MachineKeySessionSecurityTokenHandler, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
        <samlSecurityTokenRequirement>
          <nameClaimType value="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier"/>
        </samlSecurityTokenRequirement>
       </add>
      <remove type="System.IdentityModel.Tokens.SessionSecurityTokenHandler, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    </securityTokenHandlers>
  </identityConfiguration>
</system.identityModel>

但是,这似乎没有效果。我的 MVC 应用程序仍然报告一个空白的 ClaimsIdentity.Name 字段,并且 ClaimsIdentity.NameClaimType 仍然是:

http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name

在将现有声明映射到 ClaimsIdentity.Name 字段时,我的 Web.Config 应该是什么样子?

【问题讨论】:

    标签: asp.net authentication asp.net-mvc-5


    【解决方案1】:

    我发现,使用以下 securityTokenHandlers 部分,我需要基于我的 ADFS 系统中的 SAML 2.0 有效负载:

    <securityTokenHandlers>
      <add type="System.IdentityModel.Services.Tokens.MachineKeySessionSecurityTokenHandler, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <remove type="System.IdentityModel.Tokens.SessionSecurityTokenHandler, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <remove type="System.IdentityModel.Tokens.Saml2SecurityTokenHandler, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <add type="System.IdentityModel.Tokens.Saml2SecurityTokenHandler, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
        <samlSecurityTokenRequirement>
          <nameClaimType value="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname"/>
        </samlSecurityTokenRequirement>
      </add>
    </securityTokenHandlers>
    

    由于没有配置 Saml 令牌处理程序,我完全不确定默认 web.config 是如何使用声明的。也许源代码中的某些内容会执行一些默认行为...

    【讨论】:

      猜你喜欢
      • 2017-04-18
      • 2010-10-18
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-03
      • 1970-01-01
      相关资源
      最近更新 更多