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?
namespace parentnchild.Controllers
{
public class InitialController : Controller
{
private readonly ApplicationDbContext _db;
private readonly UserManager<IdentityUser> _userManager;

public InitialController(ApplicationDbContext context, UserManager<IdentityUser> userManager)
{
_db = context;
_userManager = userManager;
}

/* GET METHOD */
/* This is the initial view in our flow of registering a property as a parent. It just displays the search input box without any other information on the page */
public IActionResult InitialView()
{

if (_userManager.IsInRoleAsync(User, "NORTH"))
{
List<LIS> results = _db.LIS.Where(s => s.Sector.Contains("NORTH")).ToList();
return View(results);
}
else if (_userManager.IsInRoleAsync(User, "SOUTH"))
{

List<LIS> results = _db.LIS.Where(s => s.Sector.Contains("SOUTH")).ToList();
return View(results);
}
else if (_userManager.IsInRoleAsync(User, "EAST"))
{

List<LIS> results = _db.LIS.Where(s => s.Sector.Contains("EAST")).ToList();
return View(results);
}
else if (_userManager.IsInRoleAsync(User, "WEST"))
{

List<LIS> results = _db.LIS.Where(s => s.Sector.Contains("WEST")).ToList();
return View(results);
}
else
{
List<LIS> results = _db.LIS.ToList();
return View(results);
}

}
}
namespace parentnchild.Controllers
{
public class InitialController : Controller
{
private readonly ApplicationDbContext _db;
private readonly UserManager<IdentityUser> _userManager;

public InitialController(ApplicationDbContext context, UserManager<IdentityUser> userManager)
{
_db = context;
_userManager = userManager;
}

/* GET METHOD */
/* This is the initial view in our flow of registering a property as a parent. It just displays the search input box without any other information on the page */
public IActionResult InitialView()
{

if (_userManager.IsInRoleAsync(User, "NORTH"))
{
List<LIS> results = _db.LIS.Where(s => s.Sector.Contains("NORTH")).ToList();
return View(results);
}
else if (_userManager.IsInRoleAsync(User, "SOUTH"))
{

List<LIS> results = _db.LIS.Where(s => s.Sector.Contains("SOUTH")).ToList();
return View(results);
}
else if (_userManager.IsInRoleAsync(User, "EAST"))
{

List<LIS> results = _db.LIS.Where(s => s.Sector.Contains("EAST")).ToList();
return View(results);
}
else if (_userManager.IsInRoleAsync(User, "WEST"))
{

List<LIS> results = _db.LIS.Where(s => s.Sector.Contains("WEST")).ToList();
return View(results);
}
else
{
List<LIS> results = _db.LIS.ToList();
return View(results);
}

}
}
4 Replies
statto1974
statto19747mo ago
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 property
Bulelani Botman
Bulelani Botman7mo ago
I 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?
statto1974
statto19747mo ago
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")
Bulelani Botman
Bulelani Botman7mo ago
I will definitely try that! I come back later with an answer
Want results from more Discord servers?
Add your server