'blazor wasm using new Claim("roles", role) cause @attribute [Authorize(Roles = "admin")] not work

when I use new claims.Add(new Claim(ClaimTypes.Role, role)); @attribute [Authorize(Roles = "admin")] and AuthorizeView Roles="admin" works correctly. But when I use claims.Add(new Claim("Roles", role)); AuthorizeView(Roles="admin") doesn't work and show an error in browser console:

"RolesAuthorizationRequirement:User.IsInRole must be true for one of the following roles: (admin)"

I tested options.TokenValidationParameters.RoleClaimType = "Roles"; in AddIdentity() and RoleClaimType = "Roles" in TokenValidationParameters but those don't work.

How can we use our "Roles" key instead of ClaimTypes.Role?

My project is here : http://github.com/mammadkoma/Attendance



Solution 1:[1]

AuthenticationStateProvider :

return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity(ParseClaimsFromToken(token), "jwtAuthType", ClaimTypes.Name, "Roles")));

var authenticatedUser = new ClaimsPrincipal(new ClaimsIdentity(ParseClaimsFromToken(token), "jwtAuthType", ClaimTypes.Name, "Roles"));

https://github.com/mammadkoma/Attendance/blob/master/Attendance/Client/Configs/AuthStateProvider.cs

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1