2

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. enter image description here


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.

aBlaze
  • 2,436
  • 2
  • 31
  • 63

1 Answers1

3

I believe you will need to set the correct nameClaimType in your web.config:

https://learn.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.

spottedmahn
  • 14,823
  • 13
  • 108
  • 178
Jason G.
  • 790
  • 1
  • 6
  • 17
  • Thank you for your suggestion. I tried this and am getting the error "The configuration section 'system.identityModel' cannot be read because it is missing a section declaration". Does this work for .NET Framework 4.7.1? – aBlaze Mar 24 '18 at 21:43
  • According to this answer, https://stackoverflow.com/a/6384148/9268003, I do already have a non-empty section in my configuration file, which should have prevented the error. – aBlaze Mar 24 '18 at 21:46
  • You will need to declare the section under configSections see the top of the example here https://learn.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/windows-identity-foundation/system-identitymodel – Jason G. Mar 24 '18 at 21:53
  • Thank you for that second link - it definitely got rid of the error! However, I am still seeing `User.Identity.Name = null`. I have updated the original question to include the edits (because they are too long to fit here). Please let me know if I'm still missing something. – aBlaze Mar 24 '18 at 22:18
  • I am glad you posted you config I was going to ask to see that next :) It looks correct. How about trying to pull it out similar to this method https://stackoverflow.com/a/33773222/1435302 specifying ClaimTypes.Name – Jason G. Mar 24 '18 at 22:55
  • Thanks for your help! I just edited your answer to add an additional step which I needed to fix the issue of `User.Identity.Name` being null, and I accepted the answer. – aBlaze Mar 24 '18 at 23:01