I've been looking around how to use roles in SPA, and the basic project that Visual Studio (2019) creates is quite fine, however [Authorize(Roles="")]
does not work properly, so I can use Authorize attribute to check if user is actually logged in but not the role.
My approach now in controllers is to get the user and use IsInRole()
, but this seems like a big overhead since I should have his token already which should include the role.
ClaimsPrincipal currentUser = this.User;
var currentUserId = currentUser.FindFirst(ClaimTypes.NameIdentifier).Value;
User user = await _userManager.FindByIdAsync(currentUserId);
if (await _userManager.IsInRoleAsync(user, "Admin"))
...