This documentation is archived and is not being maintained.

Roles.RemoveUserFromRoles Method

Removes the specified user from the specified roles.

Namespace:  System.Web.Security
Assembly:  System.Web (in System.Web.dll)

public static void RemoveUserFromRoles(
	string username,
	string[] roleNames


Type: System.String
The user to remove from the specified roles.
Type: System.String[]
A string array of role names to remove the specified user from.


One of the roles in roleNames is null.


username is null.


One of the roles in roleNames is an empty string or contains a comma (,).


username is an empty string or contains a comma (,).


roleNames contains a duplicate element.


Role management is not enabled.

The RemoveUserFromRoles method calls the default role provider to remove the specified user from the specified roles at the data source.

If the application is configured to use the SqlRoleProvider class, the database updates that are performed during the call to the RemoveUserFromRoles method are made within a transaction. If an error is encountered, such as a user name that is not in a specified role, the transaction is rolled back and no updates are performed.

The following code example uses the RemoveUserFromRoles method to remove a user from one or more roles. For an example of a Web.config file that enables role management, see Roles.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Web.UI" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script runat="server">

string[] rolesArray;
MembershipUserCollection users;

public void Page_Load()
  Msg.Text = "";

  if (!IsPostBack)
    // Bind roles to ListBox.

    users = Membership.GetAllUsers();
    UsersListBox.DataSource = users;

public void UsersListBox_OnSelectedIndexChanged(object sender, EventArgs args)
  // Bind users to ListBox.

  rolesArray = Roles.GetRolesForUser(UsersListBox.SelectedItem.Value);
  RolesListBox.DataSource = rolesArray;

public void RemoveUser_OnClick(object sender, EventArgs args)
  // Verify that at least a user and at least one role are selected.

  if (UsersListBox.SelectedItem ==  null)
    Msg.Text = "Please select a user.";

  int[] role_indices = RolesListBox.GetSelectedIndices();

  if (role_indices.Length == 0)
    Msg.Text = "Please select one or more roles.";

  // Create list of roles to be remove the selected user from.

  string[] rolesList = new string[role_indices.Length];

  for (int i = 0; i < rolesList.Length; i++)
    rolesList[i] = RolesListBox.Items[role_indices[i]].Value;

  // Remove the user to the selected roles.

    Roles.RemoveUserFromRoles(UsersListBox.SelectedItem.Value, rolesList);  
    Msg.Text = "User removed from Role(s).";

    // Rebind roles to ListBox.

    rolesArray = Roles.GetRolesForUser(UsersListBox.SelectedItem.Value);
    RolesListBox.DataSource = rolesArray;
  catch (HttpException e)
    Msg.Text = e.Message;

<html xmlns="" >
<title>Sample: Role Membership</title>

<form runat="server" id="PageForm">
  <h3>Role Membership</h3>
  <asp:Label id="Msg" ForeColor="maroon" runat="server" /><br />
  <table cellpadding="3" border="0">
      <td valign="top">Users:</td>
      <td valign="top"><asp:ListBox id="UsersListBox" Rows="8" DataTextField="Username" 
                                    AutoPostBack="true" runat="server" /></td>
      <td valign="top">Roles:</td>
      <td valign="top"><asp:ListBox id="RolesListBox" SelectionMode="Multiple" 
                                    runat="server" Rows="8" /></td>
      <td valign="top"><asp:Button Text="Remove User from Roles" id="RemoveUserButton"
                                   runat="server" OnClick="RemoveUser_OnClick" /></td>


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.