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

用户通过我的 Azure AD B2C Web 应用程序的身份验证后,我尝试检索 User.Identity.Name;但是,它是空的。然而,User.Identity.m_instance_claims[9],如下面的屏幕截图所示,名称确实正确。

怎么会这样?我怎样才能得到 User.Identity.Name = User.Identity.m_instance_claims[9]?

(注意后者是私有变量,不能代替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

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


更新

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

// Specify the claims to validate
TokenValidationParameters = new TokenValidationParameters
{
    NameClaimType = "name"
},

请参阅 this link 了解上述内容的使用方式。