.NET read roles from comma separated string instead of from array

277 Views Asked by At

In my JWT I have a list of roles like this: {..., role: "1,2,3", ...}. When my request hits [Authorize(Role = "1")], it returns unauthorized.

Is there a way to parse this comma separated string into an array of roles on my AddAuthentication or AddJwtBearer calls?

1

There are 1 best solutions below

0
Tore Nestenius On

You need to use this mapping:

options.ClaimActions.MapJsonKey(JwtClaimTypes.Role, JwtClaimTypes.Role);

instead of

options.ClaimActions.MapUniqueJsonKey(JwtClaimTypes.Role, JwtClaimTypes.Role);

I did write a lengthy post about these problems in a blog post here: Debugging OpenID Connect claim problems in ASP.NET Core