get current user's role
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
}
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()
.
Simplemembership in MVC4:
Getting the User's role-
var role = System.Web.Security.Roles.GetRolesForUser().Single();
To check if the user belongs to a certain role-
if (User.IsInRole("External"))