Tuesday, September 16, 2014

Add user tables with ASP.net membership






It will be in 3 steps:
Those are the following ....





Step1: how to install asp.net membership database schema in SQL Server
Introduction:

 In this article I will explain how to install asp.net Membership database schema in Sql Server.

Description:

In one of my project I got requirement like using asp.net membership concept with custom database. First of all we learn what is asp.net membership? And why we need to use this one?

ASP.NET membership gives you a built-in way to validate and store user credentials. ASP.NET membership therefore helps you manage user authentication in your Web sites. You can use ASP.NET membership with ASP.NET Forms authentication or with the ASP.NET login controls to create a complete system for authenticating users.
By Using ASP.NET membership we can create new users and passwords and we can authenticate users who visit your site. You can authenticate users programmatically, or you can use the ASP.NET login controls to create a complete authentication system that requires little or no code and we can manage passwords, which includes creating, changing, and resetting them. 

Now we will do the step by step process how to use Custom membership database.

1.    Go to the Start menu and open Run and enter%WINDIR%\Microsoft.Net\Framework\v2.0.50727\aspnet_regsql.exe and click ok it will open the Asp.net SQL Server setup wizard like this 


We can get this in alternatively go toC:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regsql.exe
After that click next you will get window like this
In this wizard we have two options
1.    Configure SQL Server for application Services
2.    Remove application services information from an existing database.
Now select first option Configure SQL Server for application services because first time we are creating database. If you want to delete already created database select second option
After selecting the first option click next now the window like this
After that enter your database server name and select database from existing list or give new name and click next you will see the window like this
After that click next button you now your aspnet membership database installation is completed and now that is ready to use with our application final window like this
Now check your database everything installed and ready to use in our application.
Everything is ready how to use that custom database in our application for that create one new web application and name it as Registration.aspx now drag and drop the one new control CreateUserWizardfrom Login Controls list that aspx page like this

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:CreateUserWizard ID="CreateUserWizard1" runat="server">
<WizardSteps>
<asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">
</asp:CreateUserWizardStep>
<asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server">
</asp:CompleteWizardStep>
</WizardSteps>
</asp:CreateUserWizard>
</div>
</form>
</body>
</html>
Now open Web.config file and write the following code
Write the connectionstring like this
<connectionStrings>
<add name="Connection" connectionString="Data Source=ICOI;UserName=; password=Initial Catalog=AspMembership" providerName="System.Data.SqlClient"/>
</connectionStrings>
After that write the following code in system.web section
<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider"connectionStringName="Connection" applicationName="SampleApplication"/>
</providers>
</membership>
<profile>
<providers>
<clear/>
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider"connectionStringName="Connection" applicationName="SampleApplication"/>
</providers>
</profile>
<roleManager enabled="false">
<providers>
<clear/>
<add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider"connectionStringName="Connection" applicationName="SampleApplication"/>
</providers>
</roleManager>
After that run your application and create user by using CreateUserWizard
Here functionality of inserting the user details and everything will take care by CreatUserWizard no need to write any coding just set the database and set the conditions in web.config file that is enough. Now your program will work perfectly  


Step2: CreateUserWizard control to create user accounts in asp.net membership


Introduction:
In this article I will explain how to create users using CreateUserWizard using asp.net membership concept.

Description: 

In previous post I explained clearly . After install asp.net membership database check your database everything installed and ready to use in our application or not.  

Now open visual studio and create new web application after that drag and drop the new controlCreateUserWizard from Login Controls section into aspx page and check the aspx page code that would be like this 


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Create Users Using CreateUserWizard Control</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:CreateUserWizard ID="CreateUserWizard1" runat="server">
<WizardSteps>
<asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">
</asp:CreateUserWizardStep>
<asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server">
</asp:CompleteWizardStep>
</WizardSteps>
</asp:CreateUserWizard>
</div>
</form>
</body>
</html>
 


After that run your application and create users by using CreateUserWizard control.

Here the functionality of insert user details and everything will take care by CreatUserWizard no need to write any coding just set the database and set the conditions in web.config file that is enough it will work perfectly.

During the time of create user if you get error like “Password length minimum: 7. Non-alphanumeric characters required: 1.” check this post solve password length minimum: 7 problem. This problem because of our password basically asp.net membership concept will accept the password only if it contains mixed of alphanumeric and special characters.

 

If you observe above demo in that we need to enter different options like UserName, password (strong password like mixed of alphanumeric and special characters), Email (Unique Email), Security Question and answer to register new user. 

If we don’t want security question and answer options and we need to allow users to enter his own password without any restrictions for that we need to configure some properties in web.config file likerequiresQuestionAndAnswer="false", minRequiredPasswordLength="6" andminRequiredNonalphanumericCharacters="0" to set those properties check this post solve password length problem


Step3:Asp.net login control to check user details using Membership concept
Introduction: 

In this article I will explain how to use Login Control to validate user details using asp.net membership. 

Description: 

In previous post I explained clearly how to install asp.net membership database schema in SQL Serverand create users using CreateUserWizard in asp.net membership. Here I am using these two concepts to validate user details with Login Control using asp.net membership in our login page. 


In visual studio toolbox we have Login control in Login list section this control contain built in functionalities like validate username, password and check the user details against asp.net membership. We can use this control directly in login page instead of writing huge code in your login page with username, password textbox controls etc with asp.net membership database.

To install asp.net membership database schema in sql server check this post install membership database in SQL Server and to create user accounts check this post user accounts with CreateUserWizard.

After completion of database setup and user accounts creation open visual studio and create new web application after that right click on your application and Add new item select WebForm and give name asLogin.aspx (If you want another name you can give another name also that is your wish). Now drag and drop the new control Login from Login Controls section into Login.aspx page after that check your aspx page code that would be like this


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Login Control to validate User Details</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Login ID="Login1" runat="server" DestinationPageUrl="~/Default.aspx">
</asp:Login>
</div>
</form>
</body>
</html>
Now open Default.aspx page drag and drop the Login status control and one label to our Default.aspxpage will be like this 


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Login Control to validate User Details</title>
</head>
<body>
<form id="form1" runat="server">
<table align="center" width="200px">
<tr>
<td>
</td>
<td>
 <asp:LoginStatus ID="LoginStatus1" runat="server" />
</td>
</tr>
<tr>
<td><asp:Label ID="lblResult" runat="server" Font-Bold="true"/></td>
<td></td>
</tr>
</table>
</form>
</body>
</html>
Now write the following code in Default.aspx code behind to get login username
protected void Page_Load(object sender, EventArgs e)
{
lblResult.Text = "Welcome "+ Page.User.Identity.Name;
}
Now open Web.config file and change authentication mode to Forms and deny permissions for anonymous access

<authentication mode="Forms"/>
<authorization>
<deny users="?"/>
</authorization>
 


Now run your application and check user credentials with asp.net membership database 
Demo




Source:


http://www.aspdotnet-suresh.com/2011/12/createuserwizard-control-to-create-user.html

No comments:

Post a Comment