Azure AD B2C: User.Identity.Name is null, but User.Identity.m_instance_claims[9] has the name

女生的网名这么多〃 提交于 2021-02-04 19:40:08

问题


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.

How can this be? How can I get User.Identity.Name = User.Identity.m_instance_claims[9]?

(Note that the latter is a private variable, and it cannot be used as a substitute for User.Identity.Name.


UPDATE

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>

Unfortunately, this still gives User.Identity.Name = null.


回答1:


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


UPDATE

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.



来源:https://stackoverflow.com/questions/49469979/azure-ad-b2c-user-identity-name-is-null-but-user-identity-m-instance-claims9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!