Roles.AddUserToRole Method

Adds the specified user to the specified role.

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

public static void AddUserToRole (
	string username,
	string roleName
public static void AddUserToRole (
	String username, 
	String roleName
public static function AddUserToRole (
	username : String, 
	roleName : String
Not applicable.



The user name to add to the specified role.


The role to add the specified user name to.

Exception typeCondition


roleName is a null reference (Nothing in Visual Basic).


username is a null reference (Nothing in Visual Basic).


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


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


Role management is not enabled.

The AddUserToRole method calls the default role provider to associate the specified user with the specified role at the data source.


User names and role names cannot contain commas.

The following code example adds a user to 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;
MembershipUserCollection users;

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

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

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

    // Bind users to ListBox.

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

public void AddUser_OnClick(object sender, EventArgs args)
  // Verify that a user and a role are selected.

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

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

  // Add the user to the selected role.

    Roles.AddUserToRole(UsersListBox.SelectedItem.Value, RolesListBox.SelectedItem.Value);  
    Msg.Text = "User added to Role.";
  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" runat="server" Rows="8" /></td>
      <td valign="top">Users:</td>
      <td valign="top"><asp:ListBox id="UsersListBox" DataTextField="Username" 
                                  Rows="8" runat="server" /></td>
      <td valign="top"><asp:Button Text="Add User to Role" id="AddUserButton"
                                 runat="server" OnClick="AddUser_OnClick" /></td>


Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions