C#C
C#3y 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": "Test.User@test.com",
  "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;
        }

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?
Was this page helpful?