This documentation is archived and is not being maintained.

Roles.RemoveUsersFromRole Method

Removes the specified users from the specified role.

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

public static void RemoveUsersFromRole(
	string[] usernames,
	string roleName


Type: System.String[]

A string array of user names to remove from the specified roles.

Type: System.String

The name of the role to remove the specified users from.


roleName is null.


One of the user names in usernames is null.


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


One of the user names in usernames is an empty string or contains a comma (,).


usernames contains a duplicate element.


Role management is not enabled.

The RemoveUsersFromRole method calls the default role provider to remove the specified users from the specified role at the data source.

If the application uses the SqlRoleProvider class, the database updates that are performed during the call to the RemoveUsersFromRoles 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 RemoveUsersFromRole method to remove one or more users from a role. 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;
string[] users;

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

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

    rolesArray = Roles.GetAllRoles();
    RolesListBox.DataSource = rolesArray;

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

  users = Roles.GetUsersInRole(RolesListBox.SelectedItem.Value);
  UsersListBox.DataSource = users;

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

  int[] user_indices = UsersListBox.GetSelectedIndices();

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

  if (RolesListBox.SelectedItem == null)
    Msg.Text = "Please select a role.";

  // Create list of users to be removed from the selected role.

  string[] usersList = new string[user_indices.Length];

  for (int i = 0; i < usersList.Length; i++)
    usersList[i] = UsersListBox.Items[user_indices[i]].Value;

  // Remove the users from the selected role. 

    Roles.RemoveUsersFromRole(usersList, RolesListBox.SelectedItem.Value);  
    Msg.Text = "User(s) removed from Role.";

    // Rebind users to ListBox.

    users = Roles.GetUsersInRole(RolesListBox.SelectedItem.Value);
    UsersListBox.DataSource = users;
  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">Roles:</td>
      <td valign="top"><asp:ListBox id="RolesListBox" AutoPostBack="true" 
                                    runat="server" Rows="8" /></td>
      <td valign="top">Users:</td>
      <td valign="top"><asp:ListBox id="UsersListBox" Rows="8" 
                                    SelectionMode="Multiple" runat="server" /></td>
      <td valign="top"><asp:Button Text="Remove User(s) from Role" id="RemoveUsersButton"
                                   runat="server" OnClick="RemoveUsers_OnClick" /></td>


Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0