Membership in ASP.Net applications - part 4
This is the fourth post in a series of posts regarding ASP.Net built in membership functionality,providers,controls. You can read the first one here .You can read the second post here . You can read the third post here.
In this post I will show you how to add users programmatically to a role. In the third post we saw how to get users in a specific role.I will also show you how to delete a user and a role programmatically.
1) Launch Visual Studio 2005,2008/2010. Express editions will work fine. I am using Visual Studio 2010 Ultimate edition.
2) Create an empty asp.net web site. Choose an appropriate name.
3) Add an item to your website, a web form. Leave the default name.
4) Now go to the Visual Studio menu and choose Website->ASP.NET configurationYou will see a new web page loading. Refresh your solution in the Solution Explorerwindow. You will see the App_Data special folder added to your solution and inside the special folder you will see the ASPNETDB.MDF database.This is a SQL database.
5) Go back to your web configuration web page that was loaded when we clicked theWebsite->ASP.NET configuration.Choose Forms Authetication
6) Choose Security and enable roles. When you do that you will see changes in theweb.config file. A new line will be added.<roleManager enabled="true" />
7) Add a new role called e.g "friends".Now we must add some users to these roles.In the security tab (in the web environment), click "Create user". All this data is saved into the ASPNETDB.MDF database.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();
}
In the Page_Load event handling routine type,
if (!Page.IsPostBack)
{
GetExistingUsers();
}
16) In the Button1_Click() event handling routine type,
try
{
Membership.DeleteUser(DropDownList1.SelectedItem.Text);Label1.Text =
"<em>" + DropDownList1.SelectedItem.Text.ToString() + "</em>" +" has been deleted";GetExistingUsers();
}
catch (Exception ex){
Label1.Text = ex.Message;
}
I am using the DeleteUser method to delete the user.
Run your application and select a user from the dropdownlist control. Hit the button. The selected user will be deleted.
17) Let's do something very similar when deleting the role. Add a new item to your site, a web form. Name it DeleteRole.aspx
I know we repeat some code in these posts but I think it will be of great benefit to people to type a few times the code.
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 GetExistingRoles()
{
DropDownList1.DataSource = Roles.GetAllRoles();DropDownList1.DataBind();
}
In the Page_Load event handling routine type,
if (!Page.IsPostBack)
{
GetExistingRoles();
}
16) In the Button1_Click() event handling routine type,
try
{
string[] myusers = Roles.GetUsersInRole(DropDownList1.SelectedItem.Text);int length = myusers.Length;if (length == 0){
Roles.DeleteRole(DropDownList1.SelectedItem.Text);Label1.Text =
"<em>" + DropDownList1.SelectedItem.Text.ToString() + "</em>" +" has been deleted";GetExistingRoles();
}
elseLabel1.Text = "you cannot delete a role that has existing users attached to it";}
catch (Exception ex){
Label1.Text = ex.Message;
}
I am using the DeleteRole method to delete the role.Before that I must check to see if there are any users under the role.
Run your application and select a role from the dropdownlist control. Hit the button. The selected role will be deleted,unless it has users.
Hope it helps!!!