Alex Rivera | Logout

get current user's role

Asked 2010-01-14T23:58:29.467
37

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.

Edit
Report

1 Answer

30

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

answered 2010-01-15T00:20:02.927

Your Answer