Trying To Use The UserManager.IsInRoleAsync() Function In My Controller
I'm trying to retrieve different information based on a user who is login in but which what role? I am not getting anywhere, Visual Studio is saying I cannot convert from "System.Security.Claims.ClaimsPrincipal" to "Microsoft.AspNetCore.Identity.IdentityUser". Is there something I am missing?
4 Replies
IsInRoleAsync is expectinging an IdentityUser. The inherited User property from Controller is of type ClaimsPrincipal. You need to get the IdentityUser from _userManger first using one of the claims from the ClaimsPrincipal.
var user = await _userManager.FindByIdAsync(id);
Where id is a variable pulled out of one of the claims in the User propertyI saw this using gemini but I thought it was probably mad.
I assumed that the user since she/he is already login I would'nt have to search for them once again?
You can use the IsInRole method of ClaimsPrincipal rather than doing a round trip to the database, but need to check that the user's roles are being added as claims
User.IsInRole("NORTH")
I will definitely try that! I come back later with an answer