Is there any way to get the explicit role that a user belongs to in my controller? This assumes using ASP.NET Membership and Role Providers. \"IsInRole\" doesn\'t work - I n
if you want to get the role name of any user, then simply use following two functions in the controller you want to perform action .
public ApplicationSignInManager SignInManager
{
get { return _signInManager ?? HttpContext.GetOwinContext().Get(); }
private set { _signInManager = value; }
}
public ApplicationUserManager UserManager
{
get { return _userManager ?? HttpContext.GetOwinContext().GetUserManager(); }
private set { _userManager = value; }
}
and then in the action , use the following line .
var role = UserManager.GetRoles(UserId);
note that here UserId is the id of ApplicationUser.
this role will be IList
if your application logic is such that a user can be in only one role then you can access it by
string roleName = role[0];