Get claims and subscription in Web Api Controller (.Net Core 2.1)
No need to cast, at least if you are using .Net Core 3.1
. Simply access the values like this from a Controller:
var nameIdentifier = User.FindFirst(ClaimTypes.NameIdentifier);
var name = User.FindFirst(ClaimTypes.Name);
var givenName = User.FindFirst(ClaimTypes.GivenName);
var surname = User.FindFirst(ClaimTypes.Surname);
var email = User.FindFirst(ClaimTypes.Email);
var mobilePhone = User.FindFirst(ClaimTypes.MobilePhone);
var authenticationMethod = User.FindFirst(ClaimTypes.AuthenticationMethod);
var emails = User.FindFirst("emails");
From an access_token you can read values like this:
var handler = new JwtSecurityTokenHandler();
var jwtSecurityToken = handler.ReadJwtToken(adb2cTokenResponse.access_token);
var givenName = jwtSecurityToken.Claims.First(claim => claim.Type == JwtRegisteredClaimNames.GivenName).Value;
var familyName = jwtSecurityToken.Claims.First(claim => claim.Type == JwtRegisteredClaimNames.FamilyName).Value;
//Unless Alternate email have been added in Azure AD there will only be one email here.
//TODO Handle multiple emails
var emails = jwtSecurityToken.Claims.First(claim => claim.Type == ADB2CJwtRegisteredClaimNames.Emails).Value;
public struct ADB2CJwtRegisteredClaimNames
{
public const string Emails = "emails";
public const string Name = "name";
}
Try casting HttpContext.User.Identity
as ClaimsIdentity
.
claimsIdentity = User.Identity as ClaimsIdentity;
// alternatively
// claimsIdentity = HttpContext.User.Identity as ClaimsIdentity;
// get some claim by type
var someClaim = claimsIdentity.FindFirst("some-claim");
// iterate all claims
foreach (var claim in claimsIdentity.Claims)
{
System.Console.WriteLine(claim.Type + ":" + claim.Value);
}
Here are the .NET Core specific docs that support the HttpContext.User.Identity
property.
- HttpContext
- HttpContext.User
- ClaimsPrincipal
- ClaimsPrincipal.Identity