Creating Users in authentication

In this we see how we add membership and role to database. And how we create user and roles so that authenticated user can see the website.

In this we create user using membership and roles.

Security is very access for a website. With the help of this we can restrict access to our website to the authenticated user only.

Following are the steps to create user:

1. Add membership and roles and to the database.

For this open command prompt of visual studio 2008. Then type the following

 aspnet_regsql -S dell -U sa -P sa -d Employeedetail -A m

This will add membership to your database. Similarly for role
  aspnet_regsql -S dell -U sa -P sa -d Employeedetail -A r

Here dell is the server name,
sa is userid
sa is password
and Employeedetail is database name

2. Provide membership and role to the website.

First we add connectionstring to the web.config file:

<add name="con" connectionString="server=dell;initial catalog=Employeedetail;uid=sa;pwd=sa" providerName="System.Data.SqlClient"/>

Now we add membership and role in the web.config file:
Add it inside system.web

<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="con"/>
<roleManager enabled="true">
<add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="con"/>

Here, name and type will come from the machine.config file which is located in c drive->windows -> microsoft .net -> framework -> v2.0.5 ->config -> machine.config

from that file search the membership and role and copy the name and type from that location.

3. Change the authentication mode in web.config file:

<authentication mode="Forms"/>

** every step should be done very carefully otherwise the website will not run properly.

Now come to the website. From the top of the website their is a website tab expand it you will find configuration.
A window is open select security from that window.

Now create users.
Password must contain one alphabet, one number and one special character. eg. @123sugandha

4. add 2 page to the website say default.aspx and welcome.aspx

In default.aspx take a login control and set its DestinationPageUrl property to welcome.aspx.

In welcome.aspx add the follwing in source:

<form id="form1" runat="server">
<h4>Welocme !!!!!!!
<asp:LoginName ID="LoginName1" runat="server" />

Now run the website with default.aspx as startup page.

This will only allow the registered user to go to welcome page.

Creating Roles of the user created:

for this create a label, 1 textbox , 1 button and 1 listbox.

Do the following code:

protected void btnroleDB_Click(object sender, EventArgs e)
listbox_roleinDb.DataSource = Roles.GetAllRoles();


For this you have to add System.Web.Security namespace.

In this we have created a role of the name mentioned in textbox and then add that role in the listbox.


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: