Create User In ASP.NET Membership

To create a user in our application by using ASP.NET Membership we need the following steps to complete this process.

Step 1: 
Firstly, open visual studio, then go to File Menu and click New -> Web Site.

open new site

Step 2: 
After open the new empty website and add a new item Login.aspx in Registration inside Solution Explorer.

Add item

Step 3: 
Then write Login.aspx page code for creating page in our web site. The code is given below,

  1. <form id="form1" runat="server">  
  2.     <h3>Create New User</h3>  
  3.     <asp:Label id="Msg" ForeColor="maroon" runat="server" />  
  4.     <br />  
  5.     <table cellpadding="3" border="0">  
  6.         <tr>  
  7.             <td></td>  
  8.             <td colspan="2">  
  9.                 <b>Sign Up for New User Account</b>  
  10.             </td>  
  11.         </tr>  
  12.         <tr>  
  13.             <td>UserName:</td>  
  14.             <td>  
  15.                 <asp:TextBox ID="txtUserName" runat="server"/>  
  16.             </td>  
  17.             <td>  
  18.                 <asp:RequiredFieldValidator ID="rqfUserName" runat="server" ControlToValidate="txtUserName" Display="Dynamic" ErrorMessage="Required" ForeColor="Red"/>  
  19.             </td>  
  20.         </tr>  
  21.         <tr>  
  22.             <td>Password:</td>  
  23.             <td>  
  24.                 <asp:TextBox ID="txtPwd" runat="server" TextMode="Password"/>  
  25.             </td>  
  26.             <td>  
  27.                 <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtPwd" Display="Dynamic" ErrorMessage="Required" ForeColor="Red"/>  
  28.             </td>  
  29.         </tr>  
  30.         <tr>  
  31.             <td>Confirm Password:</td>  
  32.             <td>  
  33.                 <asp:TextBox ID="txtCnfPwd" runat="server" TextMode="Password"/>  
  34.             </td>  
  35.             <td>  
  36.                 <asp:RequiredFieldValidator id="PasswordConfirmRequiredValidator" runat="server" ControlToValidate="txtCnfPwd" ForeColor="red" Display="Dynamic" ErrorMessage="Required" />  
  37.                 <asp:CompareValidator id="PasswordConfirmCompareValidator" runat="server" ControlToValidate="txtCnfPwd" ForeColor="red" Display="Dynamic" ControlToCompare="txtPwd" ErrorMessage="Confirm password must match password." />  
  38.             </td>  
  39.         </tr>  
  40.         <tr>  
  41.             <td>Email:</td>  
  42.             <td>  
  43.                 <asp:TextBox ID="txtEmail" runat="server"/>  
  44.             </td>  
  45.             <td>  
  46.                 <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtEmail" Display="Static" ErrorMessage="Required" ForeColor="Red"/>  
  47.             </td>  
  48.         </tr>  
  49.         <tr>  
  50.             <td>Security Question:</td>  
  51.             <td>  
  52.                 <asp:TextBox ID="txtQuestion" runat="server"/>  
  53.             </td>  
  54.             <td>  
  55.                 <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtQuestion" Display="Static" ErrorMessage="Required" ForeColor="Red"/>  
  56.             </td>  
  57.         </tr>  
  58.         <tr>  
  59.             <td>Security Answer:</td>  
  60.             <td>  
  61.                 <asp:TextBox ID="txtAnswer" runat="server"/>  
  62.             </td>  
  63.             <td>  
  64.                 <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="txtAnswer" Display="Static" ErrorMessage="Required" ForeColor="Red"/>  
  65.             </td>  
  66.         </tr>  
  67.         <tr>  
  68.             <td></td>  
  69.             <td>  
  70.                 <asp:Button ID="btnSubmit" runat="server" Text="Create User" onclick="btnSubmit_Click" />  
  71.             </td>  
  72.         </tr>  
  73.         <tr>  
  74.             <td colspan="3">  
  75.                 <asp:Label ID="lblResult" runat="server" Font-Bold="true"/>  
  76.             </td>  
  77.         </tr>  
  78.     </table>  
  79. </form>  
Step 4: After that write Submit button code in Login.aspx.cs page.The code is given below,
  1. protected void btnSubmit_Click(object sender, EventArgs e) {  
  2.     MembershipCreateStatus createStatus;  
  3.     MembershipUser user = Membership.CreateUser(txtUserName.Text, txtPwd.Text, txtEmail.Text, txtQuestion.Text, txtAnswer.Text, trueout createStatus);  
  4.     switch (createStatus) {  
  5.         //This Case Occured whenver User Created Successfully in database  
  6.         case MembershipCreateStatus.Success:  
  7.             lblResult.ForeColor = Color.Green;  
  8.             lblResult.Text = "The user account was successfully created";  
  9.             txtUserName.Text = string.Empty;  
  10.             txtEmail.Text = string.Empty;  
  11.             txtQuestion.Text = string.Empty;  
  12.             txtAnswer.Text = string.Empty;  
  13.             break;  
  14.             // This Case Occured whenver we send duplicate UserName  
  15.         case MembershipCreateStatus.DuplicateUserName:  
  16.             lblResult.ForeColor = Color.Red;  
  17.             lblResult.Text = "The user with the same UserName already exists!";  
  18.             break;  
  19.             //This Case Occured whenver we give duplicate mail id  
  20.         case MembershipCreateStatus.DuplicateEmail:  
  21.             lblResult.ForeColor = Color.Red;  
  22.             lblResult.Text = "The user with the same email address already exists!";  
  23.             break;  
  24.             //This Case Occured whenver we send invalid mail format  
  25.         case MembershipCreateStatus.InvalidEmail:  
  26.             lblResult.ForeColor = Color.Red;  
  27.             lblResult.Text = "The email address you provided is invalid.";  
  28.             break;  
  29.             //This Case Occured whenver we send empty data or Invalid Data  
  30.         case MembershipCreateStatus.InvalidAnswer:  
  31.             lblResult.ForeColor = Color.Red;  
  32.             lblResult.Text = "The security answer was invalid.";  
  33.             break;  
  34.             // This Case Occured whenver we supplied weak password  
  35.         case MembershipCreateStatus.InvalidPassword:  
  36.             lblResult.ForeColor = Color.Red;  
  37.             lblResult.Text = "The password you provided is invalid. It must be 7 characters long and have at least 1 special character.";  
  38.             break;  
  39.         default:  
  40.             lblResult.ForeColor = Color.Red;  
  41.             lblResult.Text = "There was an unknown error; the user account was NOT created.";  
  42.             break;  
  43.     }  
  44. }  
Step 5: Then we set connection string and membership provider in web.config file in our web site. The same is given below,

This is for Connection

  1. <connectionStrings>  
  2.     <add name="dbconnection" connectionString="Data Source=MCNDESKTOP27\SQLEXPRESS;Initial catalog=AspMembership;User ID=sa;Password=Password$2"/>  
  3. </connectionStrings>  
This is for Membership
  1. <system.web>  
  2.     <membership>  
  3.         <providers>  
  4.             <clear/>  
  5.             <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="dbconnection" applicationName="SampleApplication"/>  
  6.         </providers>  
  7.     </membership>  
  8.     <profile>  
  9.         <providers>  
  10.             <clear/>  
  11.             <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="dbconnection" applicationName="SampleApplication"/>  
  12.         </providers>  
  13.     </profile>  
  14.     <roleManager enabled="false">  
  15.         <providers>  
  16.             <clear/>  
  17.             <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="dbconnection" applicationName="SampleApplication"/>  
  18.         </providers>  
  19.     </roleManager>  
  20.     <compilation debug="true" targetFramework="4.5" />  
  21.     <httpRuntime targetFramework="4.5" />  
  22. </system.web>   
Step 6: After creating and executing the code in our web site, next we need to create membership table in ASP.NET. For that run the aspnet_regsql.exe from command prompt. So we have to open the tool at C:\Windows\Microsoft.NET\Framework64\v4.0.30319,  here v4.0.30319 is the current.net version. Right-click on v4.0.30319 folder and select "Open command menu window here". Now run Aspnet_regsql.exe and finish the process for creating database for ASP.NET Membership.
  1. Firstly, run the aspnet_regsql.exe in open command window, then setup wizard will open like the following,

    run command

  2. Then here we click next button for further steps in setup wizard. Then my window will appear like the following,

setup wizard


setup wizard 

setup wizard


setup wizard

Here we finished our process to execute of aspnet_regsql.exe command and processed the steps for creating membership table in ASP.NET. So now table is created in my assigned database. Here different type of table created in my database.

Step 7: After completing this process we will go back to visual studio and run the application by clicking F5. After debugging our application registration page will appear in browser where we enter the textbox field which is required. After inserting the correct information in the field, we click the submit button for creating the user. For successful user creation a message will appear in a label in registration page.

create user
 
usercreated 
Step 8: By proceeding this process ASP.NET Membership provider which create user information data in aspnet_Membership table.
 
result

Here, this is the data in table which we have created. The data goes into the aspnet_membership table which is created by ASP.NET membership provider.

Up Next
    Ebook Download
    View all
    Learn
    View all