How to add user to role in c#

Roles.AddUserToRole() Method
.Net framework Roles.AddUerToRole() method allow us to add the specified user to the specified role. this Roles class AddUserToRole() method exists under System.Web.Security namespace.

AddUserToRole() method require to pass two parameters named 'username' and 'roleName'. both parameters value data type is System.String.

'username' parameter value represents the user name to add to the specified role. 'roleName' property value represents the role to add the specified user name to.

AddUserToRole() method throw System.ArgumentNullException exception, if the 'roleName' is null or 'username' is null. this method throw System.ArgumentException, if 'roleName' is an empty string or contains a comma; or 'username' is an empty string or contains a comma.

this method also throw System.Configuration.Provider.ProviderException, if the Role management is not enabled or User is already assigned to the specified role.

the following c# example code demonstrate us how can we add an user to a role programmatically at run time in an application.
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<script runat="server">
    protected void Page_Load(object sender, System.EventArgs e) { 
            ListBox1.DataSource = Roles.GetAllRoles();
            ListBox2.DataSource = Membership.GetAllUsers();

    protected void ListBox1_SelectedIndexChanged(object sender, System.EventArgs e) {
        ListBox3.DataSource = Roles.GetUsersInRole(ListBox1.SelectedItem.Text.ToString());

    protected void Button1_Click(object sender, System.EventArgs e) {
        Roles.AddUserToRole(ListBox2.SelectedItem.Text.ToString(), ListBox1.SelectedItem.Text.ToString());
        ListBox3.DataSource = Roles.GetUsersInRole(ListBox1.SelectedItem.Text.ToString());
        Label1.Text = ListBox2.SelectedItem.Text.ToString() +
            " User Added in " +
            ListBox1.SelectedItem.Text.ToString() +
            " Role successfully!";

<html xmlns="">
<head runat="server">
    <title>AddUserToRole method example: programmatically adding user to role in</title>
    <form id="form1" runat="server">
        <h2>Add user to role example</h2>
        <asp:Label ID="Label1" runat="server" Font-Size="Larger" ForeColor="Crimson"></asp:Label>
        <br /><br />
        <b>All Roles</b>
        <br />
        <asp:ListBox ID="ListBox1" runat="server" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged" AutoPostBack="true"></asp:ListBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="ListBox1" Text="*"></asp:RequiredFieldValidator>
        <br /><br />
        <b>All Users</b>
        <br />
        <asp:ListBox ID="ListBox2" runat="server"></asp:ListBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="ListBox2" Text="*"></asp:RequiredFieldValidator>
        <hr />
        <b style="color:Fuchsia">All users in selected role</b>
        <br />
        <asp:ListBox ID="ListBox3" runat="server"></asp:ListBox>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Add selected user in selected role" OnClick="Button1_Click" />

Related examples