get current user's role

Viewed 92484

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 need to actually get the name of the role they are in.

8 Answers

A user can be in more than one role so you can't get the one role that the user is in, but you can easily get the list of roles a user is in.

You can use the Roles type to get the list of roles that the currently logged in user is in:

public ActionResult ShowUserRoles() {
    string[] roleNames = Roles.GetRolesForUser();
    return View(roleNames);
}

Or if you want to get the roles for an arbitrary user you can pass in the username when you call Roles.GetRolesForUser().

You can get a list of Roles from the GetRoles method. (From the link)

  string[] rolesArray;

  public void Page_Load()
  {
       RolePrincipal r = (RolePrincipal)User;
       rolesArray = r.GetRoles();
       ...//extra code
  }

I Use this code

 ((ClaimsIdentity)User.Identity).FindAll(ClaimTypes.Role).ToList()
 .OrderBy(x => x.Value == "admin" ? 1
 : x.Value == "Salesperson" ? 2 
 : x.Value == "User" ? 3 : 4).First().Value

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<ApplicationSignInManager>(); }
            private set { _signInManager = value; }
        }

        public ApplicationUserManager UserManager
        {
            get { return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); }
            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];

well i'm using .net 6 and i was going to use the UserManager in an custom Attribute but non of the answers helped me

At the end I ended up creating an service and interface and sum it up using dependency injection

my interface "IPermisisonAuthorization" :

public interface IPermissionAuthorization
{
    public List<IdentityUser> GetAllRoles(string id);
}

in my service "PermissionAuthorization" :

public class PermisisonAutorization : IPermissionAuthorization
{
    UserManager<IdentityUser> userManager;

    public PermisisonAutorization(UserManager<IdentityUser> userManager)
    {
        this.userManager = userManager;
    }

    public List<IdentityUser> GetAllRoles(string id) {
        return userManager.Users.Where(user => user.Id == id).ToList();
    }
}

in Program.cs :

builder.Services.AddTransient<IPermissionAuthorization, PermisisonAutorization>();

and in your class you can get it by HttpContext :

IPermissionAuthorization permissionAuthorization = context.HttpContext.RequestServices.GetService<IPermissionAuthorization>();

the "context" is of type "AuthorizationFilterContext" ant it should be passed by the controller, but custom attributes have them by default

Related