tags:

views:

75

answers:

3

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.

+1  A: 

Hi, 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
  }
keyboardP
A: 

I believe, as each user can be part of multiple roles, there is no direct method provided to get a users role.

But you can always write ur own function if u know that there is one-one mapping between a user and role.

Hope this helps.

Shishya
+2  A: 

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().

Eilon