gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:43:19 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com