0

I have an application that is in .net 4.7 and i am wanting to bring authentication over to use Azure AD from windows authentication. I have this working but i want to use User.Identity.Name which is currently null

I have seen that i can configure this somehow within startup.cs in configuration using NameClaimTypeReceiver

TokenValidationParameters = new TokenValidationParameters()
                {
                    ValidateIssuer = false,
                    NameClaimTypeRetriever = OnNameClaimTypeRetriever
                }

I cant see an example of writing this method anywhere OnNameClaimTypeRetriever

would you please be able to provide an example.

I am wanting to set it to OnPremisesSamAccountName which i can get via azure graph

user900566
  • 93
  • 1
  • 9

1 Answers1

0

NameClaimTypeRetriever is invoked from TokenValidationParameters.CreateClaimsIdentity(SecurityToken securityToken, string issuer) using those same parameters.

Example:

    NameClaimTypeRetriever = (SecurityToken securityToken, string issuer) =>
    {
        var validUserNameTokens = new[] { "preferred_username", JwtRegisteredClaimNames.Email };
        return validUserNameTokens.FirstOrDefault(token => (securityToken as JwtSecurityToken).Claims.Any(claim => token == claim.Type));
    }

This checks for any claim which type is either 'preferred_username' or 'email'. If not present, returning null makes CreateClaimsIdentity() use ClaimsIdentity.DefaultNameClaimType instead.

9Rune5
  • 373
  • 1
  • 4
  • 16