Azure AD B2C:User.Identity.Name 为空,但 User.Identity.m_instance_claims[9] 具有名称 [英] Azure AD B2C: User.Identity.Name is null, but User.Identity.m_instance_claims[9] has the name

查看:13
本文介绍了Azure AD B2C:User.Identity.Name 为空,但 User.Identity.m_instance_claims[9] 具有名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用户通过 Azure AD B2C Web 应用程序的身份验证后,我尝试检索 User.Identity.Name;但是,它是空的.然而,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:

https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/windows-identity-foundation/nameclaimtype

更新

除了上述之外,还缺少以下代码:

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] 具有名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆