C
C#2y ago
Avalari

❔ Claim type of JwtRegisteredClaimNames.Name unrecognized as ClaimTypes.Name

Hello, I'm working with an API that returns a token with below structure:
{
"sub": "someGuid",
"name": "TestUser#1501",
"jti": "someGuid",
"email": "[email protected]",
"http://schemas.microsoft.com/ws/2008/06/identity/claims/role": "User",
"exp": 1673632405,
"iss": "siteAPI",
"aud": "siteClient"
}
{
"sub": "someGuid",
"name": "TestUser#1501",
"jti": "someGuid",
"email": "[email protected]",
"http://schemas.microsoft.com/ws/2008/06/identity/claims/role": "User",
"exp": 1673632405,
"iss": "siteAPI",
"aud": "siteClient"
}
and using this:
public async Task LoggedIn()
{
var claims = await GetClaims();
var user = new ClaimsPrincipal(new ClaimsIdentity(claims, "jwt"));
var authState = Task.FromResult(new AuthenticationState(user));
NotifyAuthenticationStateChanged(authState);
}

private async Task<List<Claim>> GetClaims()
{
var savedToken = await _localStorage.GetItemAsync<string>(StorageConstants.Local.AuthToken);
var tokenContent = _jwtSecurityTokenHandler.ReadJwtToken(savedToken);
var claims = tokenContent.Claims.ToList();
return claims;
}
public async Task LoggedIn()
{
var claims = await GetClaims();
var user = new ClaimsPrincipal(new ClaimsIdentity(claims, "jwt"));
var authState = Task.FromResult(new AuthenticationState(user));
NotifyAuthenticationStateChanged(authState);
}

private async Task<List<Claim>> GetClaims()
{
var savedToken = await _localStorage.GetItemAsync<string>(StorageConstants.Local.AuthToken);
var tokenContent = _jwtSecurityTokenHandler.ReadJwtToken(savedToken);
var claims = tokenContent.Claims.ToList();
return claims;
}
When I try to access this.UserName = user.Identity.Name; it is always null. Am I doing something wrong here or is there a explicit conversion required?
2 Replies
jayfromengland
I recommend using the debugger to see if the values you're expecting are getting populated.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server