1

I have a list of username's in a string array . l wanna give the all selected users a new role .I can easily give that using membership But before doing that i want remove all roles assigned to selected users. How can i do that ..

or at least how can i remove all roles assigned to a particular user?

Null Pointer
  • 9,089
  • 26
  • 73
  • 118

3 Answers3

8

Roles.RemoveUserFromRoles(user.UserName, Roles.GetRolesForUser(user.UserName));

Lzh
  • 3,585
  • 1
  • 22
  • 36
3

Try this for one user

Roles.RemoveUserFromRoles

or

Roles.RemoveUsersFromRoles

for many.

Here is a good tutorial about Roles, http://www.asp.net/security/tutorials/assigning-roles-to-users-cs

zynaps
  • 471
  • 3
  • 10
  • 18
  • 2
    These do not answer the question. They are for removing specific roles, not all roles. – Lukos Apr 17 '13 at 13:26
1

I know its old...but I was just looking for this myself:

 MembershipUser mUser = Membership.GetUser((Guid)User.UserID);

 foreach (var role in Roles.GetAllRoles())
 {
      Roles.RemoveUserFromRole(mUser.UserName, role);
 }
Amit M
  • 61
  • 7
  • This will throw an exception if the user is not in *all* roles. Because RemoveUserFromRole throws an exception if the the user is not in the role specified. – Lzh Oct 27 '13 at 00:08