I created a new user and added him to the friends role. Make sure you use a strong password with 7 characters or more containing at least one non-alphanumeric character.
8) Create 2-3 more roles and 3-4 users and add them to those roles through the WAT(Web administration tool)
9) Add a label,2 dropdown list,a bulleted list and a button control on the form.
We will get the users and the roles we have so far and bind them to the dropdown list controls. In the Page_Load event handling routine type,
if (!Page.IsPostBack){
DropDownList1.DataSource = Roles.GetAllRoles();DropDownList1.DataBind();
DropDownList2.DataSource = Membership.GetAllUsers();DropDownList2.DataBind();
}
This is very easy code to follow.
10) In the bulleted list control we will bind the users in the specific role when the user selects the role from the first dropdown list.
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e){
BulletedList1.DataSource = Roles.GetUsersInRole(DropDownList1.SelectedItem.Text.ToString());BulletedList1.DataBind();
}
11) Now we need to select the role from the first dropdownlist control and the user from the second dropdownlist control.
The user in the second dropdownlist control should not be a member of the role in the first dropdownlist control.
In the Button1_Click() event handling routine type,
try
{
Roles.AddUserToRole(DropDownList2.SelectedItem.Text.ToString(), DropDownList1.SelectedItem.Text.ToString());BulletedList1.DataSource = Roles.GetUsersInRole(DropDownList1.SelectedItem.Text.ToString());BulletedList1.DataBind();
Label1.Text =
"<em>" + DropDownList2.SelectedItem.Text.ToString() + "</em>" +" has been added to the " +"<em>" + DropDownList1.SelectedItem.Text.ToString() + "</em>" +" role"; }
catch (Exception ex){
Response.Write(ex.Message);
}
12) Run your application and select any role you want. All the users of the specified role will appear. Now select a role and a user that does not belong to the role. Hit the button. That user will be added to the role.
13) Now we will see how to delete a user and a role using the Membership and Roles static classes.
14) Add a new item to your site, a web form. Name it DeleteUser.aspx.
Add a button, a label, a dropdowlist control on the form.
15) We will create a void method to get all existing users.
private void GetExistingUsers(){
DropDownList1.DataSource = Membership.GetAllUsers();DropDownList1.DataBind();
}