This documentation is archived and is not being maintained.
Roles.AddUserToRole Method
Visual Studio 2010
Adds the specified user to the specified role.
Assembly: System.Web (in System.Web.dll)
'Declaration Public Shared Sub AddUserToRole ( _ username As String, _ roleName As String _ )
Parameters
- username
- Type: System.String
The user name to add to the specified role.
- roleName
- Type: System.String
The role to add the specified user name to.
| Exception | Condition |
|---|---|
| System.ArgumentNullException | roleName is Nothing. -or- username is Nothing. |
| System.ArgumentException | roleName is an empty string or contains a comma (,). -or- username is an empty string or contains a comma (,). |
| System.Configuration.Provider.ProviderException | Role management is not enabled. -or- User is already assigned to the specified role. |
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="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 a role are selected. If UsersListBox.SelectedItem Is Nothing Then Msg.Text = "Please select a user." Return End If If RolesListBox.SelectedItem Is Nothing Then Msg.Text = "Please select a role." Return End If ' Add the user to the selected role. Try Roles.AddUserToRole(UsersListBox.SelectedItem.Value, RolesListBox.SelectedItem.Value) Msg.Text = "User added to Role." 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" 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> </tr> </table> </form> </body> </html>
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.
Show:
Note