Roles.AddUserToRoles(String, String[]) Method

Definition

Adds the specified user to the specified roles.

public:
 static void AddUserToRoles(System::String ^ username, cli::array <System::String ^> ^ roleNames);
public static void AddUserToRoles (string username, string[] roleNames);
static member AddUserToRoles : string * string[] -> unit
Public Shared Sub AddUserToRoles (username As String, roleNames As String())

Parameters

username
String

The user name to add to the specified roles.

roleNames
String[]

A string array of roles to add the specified user name to.

Exceptions

One of the roles in roleNames is null.

-or-

username is null.

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

-or-

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

-or-

roleNames contains a duplicate element.

Role management is not enabled.

Examples

The following code example adds a user to 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"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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;
    RolesListBox.DataBind();

    // Bind users to ListBox.

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


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

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

  int[] role_indices = RolesListBox.GetSelectedIndices();

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


  // Create list of roles to be add the selected users to.

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

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


  // Add the users to the selected role.

  try
  {
    Roles.AddUserToRoles(UsersListBox.SelectedItem.Value, rolesList);  
    Msg.Text = "User added to Role(s).";
  }
  catch (HttpException e)
  {
    Msg.Text = e.Message;
  } 
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Sample: Role Membership</title>
</head>
<body>

<form runat="server" id="PageForm">
  <h3>Role Membership</h3>
  <asp:Label id="Msg" ForeColor="maroon" runat="server" /><br />
  <table cellpadding="3" border="0">
    <tr>
      <td valign="top">Roles:</td>
      <td valign="top"><asp:ListBox id="RolesListBox" runat="server" SelectionMode="Multiple"
                                    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(s)" id="AddUserButton"
                                 runat="server" OnClick="AddUser_OnClick" /></td>
    </tr>
  </table>
</form>

</body>
</html>
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Web.UI" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

Dim rolesArray() As String
Dim users As MembershipUserCollection

Public Sub Page_Load()

  Msg.Text = ""

  If Not IsPostBack Then
    ' Bind roles to ListBox.

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

    ' Bind users to ListBox.

    users = Membership.GetAllUsers()
    UsersListBox.DataSource = users
    UsersListBox.DataBind()
  End If
End Sub


Public Sub AddUser_OnClick(sender As Object, args As EventArgs)

  ' Verify that a user and at least one role are selected.

  If UsersListBox.SelectedItem Is Nothing Then
    Msg.Text = "Please select one or more users."
    Return
  End If

  Dim role_indices() As Integer = RolesListBox.GetSelectedIndices()

  If role_indices.Length = 0 Then
    Msg.Text = "Please select one or more roles."
    Return
  End If


  ' Create list of roles to be add the selected users to.

  Dim rolesList(role_indices.Length - 1) As String

  For i As Integer = 0 To rolesList.Length - 1
    rolesList(i) = RolesListBox.Items(role_indices(i)).Value
  Next


  ' Add the users to the selected role.

  Try
    Roles.AddUserToRoles(UsersListBox.SelectedItem.Value, rolesList)
    Msg.Text = "User added to Role(s)."
  Catch e As HttpException
    Msg.Text = e.Message
  End Try
End Sub

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Sample: Role Membership</title>
</head>
<body>

<form runat="server" id="PageForm">
  <h3>Role Membership</h3>
  <asp:Label id="Msg" ForeColor="maroon" runat="server" /><br />
  <table cellpadding="3" border="0">
    <tr>
      <td valign="top">Roles:</td>
      <td valign="top"><asp:ListBox id="RolesListBox" runat="server" SelectionMode="Multiple"
                                    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(s)" id="AddUserButton"
                                 runat="server" OnClick="AddUser_OnClick" /></td>
    </tr>
  </table>
</form>

</body>
</html>

Remarks

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

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

Note

User names and role names cannot contain commas.

Applies to

See also