I tried searching everywhere on the web, but I can't seem to figure out this important part.
Basically, if we do a DB call each time when checking if a user belongs to a role - this will have negative effect on performance.
I saw code examples listing all user roles, e.g.
var roles = ((ClaimsIdentity)User.Identity).Claims
.Where(c => c.Type == ClaimTypes.Role)
.Select(c => c.Value);
the code can be used in controller action, it is also possible to fetch claims the same way in an Attribute Filter.
From this example I infer that Claims
come into play (seems to be most performant solution).
I tried to find out if Authorize
attribute with Roles verifies user's claims, but the official Microsoft documentation doesn't cover this bit.
AuthorizeAttribute
classSpecifies that access to a controller or action method is restricted to users who meet the authorization requirement.
Properties:
Roles
- Gets or sets the user roles that are authorized to access the controller or action method.
And that's the extent of what we have.