如何在ASP.Net MVC 5站点中设置NameClaimType?

时间:2013-11-20 20:43:09

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

我使用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

我的Web.Config应该将我现有的声明映射到ClaimsIdentity.Name字段?

1 个答案:

答案 0 :(得分:2)

我发现使用以下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使用声明。也许源代码中的某些东西会做一些默认行为...

相关问题