用户通过我的 Azure AD B2C Web 应用程序身份验证后,我尝试检索 User.Identity.Name
;然而,它是空的。然而,User.Identity.m_instance_claims[9]
(如下面的屏幕截图所示)确实具有正确的名称。
怎么会这样?如何获取 User.Identity.Name
= User.Identity.m_instance_claims[9]
?
(请注意,后者是私有(private)变量,不能用作 User.Identity.Name
的替代品。
更新
我还在 Web.config
文件中添加了以下内容:
<configuration>
<configSections>
<!--WIF 4.5 sections -->
<section name="system.identityModel" type="System.IdentityModel.Configuration.SystemIdentityModelSection, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<section name="system.identityModel.services" type="System.IdentityModel.Services.Configuration.SystemIdentityModelServicesSection, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
</configSections>
...
<system.identityModel>
<identityConfiguration>
<securityTokenHandlers>
<add type="System.IdentityModel.Tokens.SamlSecurityTokenHandler, System.IdentityModel">
<samlSecurityTokenRequirement>
<nameClaimType value="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name" />
</samlSecurityTokenRequirement>
</add>
</securityTokenHandlers>
</identityConfiguration>
</system.identityModel>
</configuration>
不幸的是,这仍然给出User.Identity.Name = null
。
最佳答案
我相信您需要在 web.config 中设置正确的 nameClaimType:
更新
除上述内容外,还缺少以下代码:
// Specify the claims to validate
TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = "name"
},
参见this link了解如何使用上述内容。
关于oauth-2.0 - Azure AD B2C : User. Identity.Name 为 null,但 User.Identity.m_instance_claims[9] 具有名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49469979/