Users and roles
The following sections explain how to use the roles API in the context of the users:
Check whether a user is in a role
The following example checks whether the user is in the specified role.
First, you get an instance of the roles and users managers. Then, to get a value indicating whether the specified user is in the specified role, you call the IsUserInRole method of the roles manager.
public bool IsUserInRole(string userName, string roleName)
{
bool isUserInRole = false;
UserManager userManager = UserManager.GetManager();
RoleManager roleManager = RoleManager.GetManager();
bool userExists = userManager.UserExists(userName);
bool roleExists = roleManager.RoleExists(roleName);
if (userExists && roleExists)
{
User user = userManager.GetUser(userName);
isUserInRole = roleManager.IsUserInRole(user.Id, roleName);
}
return isUserInRole;
}
Get all users in role
The following example returns all users in the specified role.
First, you get an instance of the roles manager. Then, to get all users in the specified role, you call the GetUsersInRole method passing the role name as an argument.
public List<User> GetUsersInRole(string roleName)
{
List<User> users = new List<User>();
RoleManager roleManager = RoleManager.GetManager();
if (roleManager.RoleExists(roleName))
{
users = roleManager.GetUsersInRole(roleName).ToList();
}
return users;
}
Get all roles for user
The following example returns all roles that the specified user is in.
First, you get an instance of the roles manager. Then, you get the user by calling the GetUser method of the users’ manager. Finally, to get all roles for the user, you call the GetRolesForUser method of the roles manager passing the ID of the user as an argument.
public List<Role> GetRolesForUser(string userName)
{
List<Role> roles = new List<Role>();
RoleManager roleManager = RoleManager.GetManager();
UserManager userManager = UserManager.GetManager();
bool userExists = userManager.UserExists(userName);
if (userExists)
{
User user = userManager.GetUser(userName);
roles = roleManager.GetRolesForUser(user.Id).ToList();
}
return roles;
}
Add user to roles
The following example adds the specified user to the specified roles.
First, you get an instance of the roles and users managers. Then, you get the user by calling the GetUser method of the users’ manager. Then, for each role in the list, you add the user in it by calling the AddUserToRole method of the roles manager. Finally, you save the changes.
public void AddUserToRoles(string userName, List<string> rolesToAdd)
{
UserManager userManager = UserManager.GetManager();
RoleManager roleManager = RoleManager.GetManager();
if (userManager.UserExists(userName))
{
User user = userManager.GetUser(userName);
foreach (var roleName in rolesToAdd)
{
if (roleManager.RoleExists(roleName))
{
Role role = roleManager.GetRole(roleName);
roleManager.AddUserToRole(user, role);
}
}
}
roleManager.SaveChanges();
}
Remove user from roles
The following example removes the specified user from the specified roles.
First, you get an instance of the roles and users managers. Then, you get the user by calling the GetUser method of the users’ manager. Then, for each role in the list, you remove the user from it by calling the RemoveUserFromRole method of the roles manager. Finally, you save the changes.
public void RemoveUserFromRoles(string userName, List<string> rolesToRemove)
{
UserManager userManager = UserManager.GetManager();
RoleManager roleManager = RoleManager.GetManager();
if (userManager.UserExists(userName))
{
User user = userManager.GetUser(userName);
foreach (var roleName in rolesToRemove)
{
if (roleManager.RoleExists(roleName))
{
Role role = roleManager.GetRole(roleName);
roleManager.RemoveUserFromRole(user, role);
}
}
}
roleManager.SaveChanges();
}
See also
Roles