Create User Programmatically Using Membership In Asp.Net

Create User Programmatically Using Membership Provider In Asp.Net

To create users or new accounts programmatically we need to use CreateUser Method of membership class.

Asp.Net Membership Create user programmatically
For this i have placed textbox controls and requiredFieldValidators to validate respective textboxes.

Read CreateUserWizard Email Verification Or Confirmation In Asp.NET to know how to create user with Email Activation validation link.


HTML SOURCE OF PAGE
<table border="0" style="font-family:Verdana;font-size:100%;">
<tr>
<td align="center" colspan="2" style="color:White;
    background-color:#507CD1;
    font-weight:bold;">Create New User</td></tr>

<tr><td align="right">User Name:</td>
<td><asp:TextBox ID="txtUserName" runat="server">
</asp:TextBox>
<asp:RequiredFieldValidator 
     ID="UserName" runat="server" 
     ControlToValidate="txtUserName" 
     ErrorMessage="User Name is required." 
     ToolTip="User Name is required.">*
</asp:RequiredFieldValidator></td></tr>

<tr><td align="right">Password:</td>
<td><asp:TextBox ID="txtPassword" runat="server" 
                 TextMode="Password">
</asp:TextBox>
<asp:RequiredFieldValidator 
     ID="Password" runat="server" 
     ControlToValidate="txtPassword" 
     ErrorMessage="Password is required." 
     ToolTip="Password is required.">*
</asp:RequiredFieldValidator></td></tr>

<tr><td align="right">Confirm Password:</td>
<td><asp:TextBox ID="txtConfirmPassword" 
                 runat="server" 
                 TextMode="Password">
</asp:TextBox>
<asp:RequiredFieldValidator 
     ID="ConfirmPassword" runat="server" 
     ControlToValidate="txtConfirmPassword" 
     ErrorMessage="Confirm Password is required." 
     ToolTip="Confirm Password is required.">*
</asp:RequiredFieldValidator></td></tr>

<tr><td align="right">E-mail:</td>
<td><asp:TextBox ID="txtEmail" runat="server">
</asp:TextBox>
<asp:RequiredFieldValidator 
     ID="Email" runat="server" 
     ControlToValidate="txtEmail" 
     ErrorMessage="E-mail is required." 
     ToolTip="E-mail is required.">*
</asp:RequiredFieldValidator></td></tr>

<tr><td align="right">Security Question:</td>
<td><asp:TextBox ID="txtQuestion" runat="server">
</asp:TextBox>
<asp:RequiredFieldValidator 
     ID="Question" runat="server" 
     ControlToValidate="txtQuestion" 
     ErrorMessage="Security question is required." 
     ToolTip="Security question is required.">*
</asp:RequiredFieldValidator></td></tr>

<tr><td align="right">Security Answer:</td>
<td><asp:TextBox ID="txtAnswer" runat="server">
</asp:TextBox>
<asp:RequiredFieldValidator 
     ID="Answer" runat="server" 
     ControlToValidate="txtAnswer" 
     ErrorMessage="Security answer is required." 
     ToolTip="Security answer is required.">*
</asp:RequiredFieldValidator></td></tr>

<tr><td align="center" colspan="2">
<asp:CompareValidator 
     ID="PasswordCompare" runat="server" 
     ControlToCompare="txtPassword" 
     ControlToValidate="txtConfirmPassword" 
     ErrorMessage="The Password and Confirmation Password must match."> 
</asp:CompareValidator></td></tr>

<tr><td align="right" colspan="2" style="color:Red;">
<asp:Button ID="btnCreateUser" runat="server" 
            Text="Create User" 
            onclick="btnCreateUser_Click"/></td></tr>
<tr><td align="center" colspan="2" style="color:Red;">
<asp:Label ID="lblMessage" runat="server">
</asp:Label></td></tr>
</table>

Write below mentioned code in Click event of Create New User Button.

C# CODE
using System.Web.Security;
protected void btnCreateUser_Click(object sender, EventArgs e)
    {
        MembershipCreateStatus status;
        MembershipUser newUser = Membership.CreateUser(txtUserName.Text, txtPassword.Text, txtEmail.Text, txtQuestion.Text, txtAnswer.Text, true, out status);
        switch (status)
        {
            case MembershipCreateStatus.Success:
                lblMessage.Text = "Account Created";
                break;
            case MembershipCreateStatus.DuplicateUserName:
                lblMessage.Text = "Username Already exists";
                break;
            case MembershipCreateStatus.DuplicateEmail:
                lblMessage.Text = "Email already registered";
                    break;
            case MembershipCreateStatus.InvalidEmail:
                    lblMessage.Text = "Invalid Email";
                    break;
            case MembershipCreateStatus.InvalidPassword:
                    lblMessage.Text = "Invalid password";
                    break;
            default:
                    lblMessage.Text = "Error occured, account was not created ";
                    break;
        }
    }

VB.NET
Protected Sub btnCreateUser_Click(sender As Object, e As EventArgs)
 Dim status As MembershipCreateStatus
 Dim newUser As MembershipUser = Membership.CreateUser(txtUserName.Text, txtPassword.Text, txtEmail.Text, txtQuestion.Text, txtAnswer.Text, True, _
  status)
 Select Case status
  Case MembershipCreateStatus.Success
   lblMessage.Text = "Account Created"
   Exit Select
  Case MembershipCreateStatus.DuplicateUserName
   lblMessage.Text = "Username Already exists"
   Exit Select
  Case MembershipCreateStatus.DuplicateEmail
   lblMessage.Text = "Email already registered"
   Exit Select
  Case MembershipCreateStatus.InvalidEmail
   lblMessage.Text = "Invalid Email"
   Exit Select
  Case MembershipCreateStatus.InvalidPassword
   lblMessage.Text = "Invalid password"
   Exit Select
  Case Else
   lblMessage.Text = "Error occured, account was not created "
   Exit Select
 End Select
End Sub



If you like this post than join us or share

Find More Articles