Alan G Rutter
Alan G Rutter
CC#
Created by Alan G Rutter on 10/28/2024 in #help
JWT, ClaimIdentity and ClaimPrincipal
Hi everyone, I have a service that creates a JWT using the JsonWebTokenHandler. When I create my claims, I am using
var claims = new List<Claim>
{
new(JwtRegisteredClaimNames.Name, userId),
new("role", userRole)
};
var claims = new List<Claim>
{
new(JwtRegisteredClaimNames.Name, userId),
new("role", userRole)
};
I add these to create a ClaimsIdentity and set the authenticationType to a string, set nameType and roleType as per the above. I am now trying to handle the refresh token which requires me to get a ClaimsPrincipal. I validate the token using JsonWebTokenHandler and use the result to instantiate a ClaimsPrincipal using the token Claims. When I inspect the ClaimsPrincipal, the AuthenticationType is set to AuthenticationType.Federated and the two claims contain the long XML schema names. In my JWTBearer options, I have set MapInboundClaims to false. If I simply use ClaimTypes.Name and ClaimTypes.Role, the claims end up in the token with the long schema names also instead of the short name even though MapInboundClaims is set to false. Can someone explain how to get a ClaimsPrincipal using the correct authentication type and short claim names.
1 replies