问题描述
在用户通过我的Azure AD B2C Web应用程序进行身份验证之后,我尝试检索 User.Identity.Name
;但是,它为null.但是,如下面的屏幕快照所示, User.Identity.m_instance_claims [9]
确实具有正确的名称.
After a user is authenticated into my Azure AD B2C web application, I attempt to retrieve User.Identity.Name
; however, it is null. Yet, User.Identity.m_instance_claims[9]
, as shown in the screenshot below, does correctly have the name.
这怎么可能?如何获得 User.Identity.Name
= User.Identity.m_instance_claims [9]
?
How can this be? How can I get User.Identity.Name
= User.Identity.m_instance_claims[9]
?
(请注意,后者是一个私有变量,不能代替 User.Identity.Name
.
(Note that the latter is a private variable, and it cannot be used as a substitute for User.Identity.Name
.
更新
我还向 Web.config
文件中添加了以下内容:
I have also added the following to the Web.config
file:
<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
.
Unfortunately, this still gives User.Identity.Name = null
.
推荐答案
我相信您需要在web.config中设置正确的nameClaimType:
I believe you will need to set the correct nameClaimType in your web.config:
更新
除上述内容外,还缺少以下代码:
In addition to the above, the following code was missing:
// Specify the claims to validate
TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = "name"
},
请参见此链接,以了解上面的用法.
See this link for how the above is being used.
这篇关于Azure AD B2C:User.Identity.Name为空,但User.Identity.m_instance_claims [9]具有名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!