Search code examples
.net-corejwtasp.net-core-webapiclaims

JWT token role claim translated wrong by DOTNET


I have a Dotnet core 6 Web Api that is receiving requests with JWT Tokens containing, among others, a role claim in the payload, like this:

{ "role": "SOME_ROLE" }

Now, I was unable to read from the User object (ClaimsPrincipal) this role. I did not have the same problem with other claims, just the role one, until I found that the problem was that instead of having the role named like in the token ("role"), it was renamed to "http://schemas.microsoft.com/ws/2008/06/identity/claims/role". That was the reason I could not find it. Does anyone know how to avoid this renaming and leave as it is in the Token?

Thanks.


Solution

  • Asp.net core can automatic mapping some claims, You can add this configuration in Program.cs(Net 6) to avoid this

    JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
    

    There are more details in this document, You can refer to it.