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

标签 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 字段?

最佳答案

我发现使用以下 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 token 处理程序,我根本不确定默认 web.config 是如何使用声明的。也许源代码中的某些内容会执行某些默认行为...

关于asp.net - 如何在 ASP.Net MVC 5 站点中设置 NameClaimType?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20106605/

相关文章:

powershell - powershell:使用参数启动程序的脚本?

c# - ASP.Net 核心 2 : Default Authentication Scheme ignored

mysql - 在 ASP.Net 应用程序中以正确的格式显示日期

jquery - 如何修复 mvc 中日期时间属性上的 'The field must be a date'

c# - 温莎城堡 : Inject child dependencies via method after dependency resolves

asp.net - 文件下载时出现 System.OutOfMemoryException

c# - asp.net 中登录页面的必填字段验证器

python - 如何在django中使用用户身份验证

c# - RouteCollection.MapRoute 在传递给方法时没有出现

c# - DetailsView 的事件 "ItemUpdating"中的 OldValues 集合始终为空