Hi makumbi,
Run the aspnet_regsql.exe from command prompt.
Forst open the tool from folder C:\Windows\Microsoft.NET\Framework64\v4.0.30319.
Note: v4.0.30319 is the .Net version.
Now run Aspnet_regsql.exe and finish the process for creating database for ASP.NET Membership.
- Firstly, run the aspnet_regsql.exe in open command window, then setup wizard will open and after that click next button to open next Wizard Page.
- Then we must select a Setup option (configure SQL server for application services). After that we click next button for further steps in setup wizard.
- Then we select a server and database, fill the details and then we click next button for further steps in setup wizard.
- Confirm Your Settings then click next button for further steps in setup wizard.
- The database has been created and modified.
HTML
Registration
<h3>Create New User</h3>
<br />
<table cellpadding="3" border="0">
<tr>
<td></td>
<td colspan="2"><b>Sign Up for New User Account</b></td>
</tr>
<tr>
<td>UserName:</td>
<td><asp:TextBox ID="txtUserName" runat="server" /></td>
<td>
<asp:RequiredFieldValidator ID="rqfUserName" runat="server" ControlToValidate="txtUserName"
Display="Dynamic" ErrorMessage="Required" ForeColor="Red" />
</td>
</tr>
<tr>
<td>Password:</td>
<td><asp:TextBox ID="txtPwd" runat="server" TextMode="Password" /></td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtPwd"
Display="Dynamic" ErrorMessage="Required" ForeColor="Red" />
</td>
</tr>
<tr>
<td>Confirm Password:</td>
<td><asp:TextBox ID="txtCnfPwd" runat="server" TextMode="Password" /></td>
<td>
<asp:RequiredFieldValidator ID="PasswordConfirmRequiredValidator" runat="server" ControlToValidate="txtCnfPwd"
ForeColor="red" Display="Dynamic" ErrorMessage="Required" />
<asp:CompareValidator ID="PasswordConfirmCompareValidator" runat="server" ControlToValidate="txtCnfPwd"
ForeColor="red" Display="Dynamic" ControlToCompare="txtPwd" ErrorMessage="Confirm password must match password." />
</td>
</tr>
<tr>
<td>Email:</td>
<td><asp:TextBox ID="txtEmail" runat="server" /></td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtEmail"
Display="Static" ErrorMessage="Required" ForeColor="Red" />
</td>
</tr>
<tr>
<td>Security Question:</td>
<td><asp:TextBox ID="txtQuestion" runat="server" /></td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtQuestion"
Display="Static" ErrorMessage="Required" ForeColor="Red" />
</td>
</tr>
<tr>
<td>Security Answer:</td>
<td><asp:TextBox ID="txtAnswer" runat="server" /></td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="txtAnswer"
Display="Static" ErrorMessage="Required" ForeColor="Red" />
</td>
</tr>
<tr>
<td></td>
<td><asp:Button ID="btnSubmit" runat="server" Text="Create User" OnClick="btnSubmit_Click" /></td>
</tr>
<tr>
<td colspan="3">
<asp:Label ID="lblResult" runat="server" Font-Bold="true" />
</td>
</tr>
</table>
Login
<asp:Login ID="Login1" runat="server" OnAuthenticate="OnAuthenticate"></asp:Login>
Home
Welcome <asp:LoginName ID="LoginName1" runat="server" Font-Bold="true" />
<br /><br />
<asp:LoginStatus ID="LoginStatus1" runat="server" />
Web.config
<configuration>
<system.web>
<authentication mode="Forms">
<forms defaultUrl="~/Home.aspx" loginUrl="~/Login.aspx" slidingExpiration="true" timeout="2880">
</forms>
</authentication>
<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="constr" applicationName="SampleApplication"/>
</providers>
</membership>
<profile>
<providers>
<clear/>
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="constr" applicationName="SampleApplication"/>
</providers>
</profile>
<roleManager enabled="false">
<providers>
<clear/>
<add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="constr" applicationName="SampleApplication"/>
</providers>
</roleManager>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<connectionStrings>
<add name="constr" connectionString="Data Source=.\SQL2019;Database=AjaxSamples;UID=sa;PWD=pass@123" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
Namespaces
C#
using System.Drawing;
using System.Web.Security;
VB.Net
Imports System.Drawing
Imports System.Web.Security
Code
C#
Registration
protected void OnSubmit(object sender, EventArgs e)
{
MembershipCreateStatus createStatus;
MembershipUser user = Membership.CreateUser(txtUserName.Text, txtPwd.Text, txtEmail.Text, txtQuestion.Text, txtAnswer.Text, true, out createStatus);
switch (createStatus)
{
case MembershipCreateStatus.Success:
lblResult.ForeColor = Color.Green;
lblResult.Text = "The user account was successfully created";
txtUserName.Text = string.Empty;
txtEmail.Text = string.Empty;
txtQuestion.Text = string.Empty;
txtAnswer.Text = string.Empty;
break;
case MembershipCreateStatus.DuplicateUserName:
lblResult.ForeColor = Color.Red;
lblResult.Text = "The user with the same UserName already exists!";
break;
case MembershipCreateStatus.DuplicateEmail:
lblResult.ForeColor = Color.Red;
lblResult.Text = "The user with the same email address already exists!";
break;
case MembershipCreateStatus.InvalidEmail:
lblResult.ForeColor = Color.Red;
lblResult.Text = "The email address you provided is invalid.";
break;
case MembershipCreateStatus.InvalidAnswer:
lblResult.ForeColor = Color.Red;
lblResult.Text = "The security answer was invalid.";
break;
case MembershipCreateStatus.InvalidPassword:
lblResult.ForeColor = Color.Red;
lblResult.Text = "The password you provided is invalid. It must be 7 characters long and have at least 1 special character.";
break;
default:
lblResult.ForeColor = Color.Red;
lblResult.Text = "There was an unknown error; the user account was NOT created.";
break;
}
}
Login
protected void OnAuthenticate(object sender, AuthenticateEventArgs e)
{
if (Membership.ValidateUser(Login1.UserName, Login1.Password))
{
FormsAuthentication.RedirectFromLoginPage(Login1.UserName, Login1.RememberMeSet);
}
else
{
Login1.FailureText = "Invalid User or password.";
}
}
Home
protected void Page_Load(object sender, EventArgs e)
{
if (!this.Page.User.Identity.IsAuthenticated)
{
FormsAuthentication.RedirectToLoginPage();
}
}
VB.Net
Registration
Protected Sub OnSubmit(ByVal sender As Object, ByVal e As EventArgs)
Dim createStatus As MembershipCreateStatus
Dim user As MembershipUser = Membership.CreateUser(txtUserName.Text, txtPwd.Text, txtEmail.Text, txtQuestion.Text, txtAnswer.Text, True, createStatus)
Select Case createStatus
Case MembershipCreateStatus.Success
lblResult.ForeColor = Color.Green
lblResult.Text = "The user account was successfully created"
txtUserName.Text = String.Empty
txtEmail.Text = String.Empty
txtQuestion.Text = String.Empty
txtAnswer.Text = String.Empty
Case MembershipCreateStatus.DuplicateUserName
lblResult.ForeColor = Color.Red
lblResult.Text = "The user with the same UserName already exists!"
Case MembershipCreateStatus.DuplicateEmail
lblResult.ForeColor = Color.Red
lblResult.Text = "The user with the same email address already exists!"
Case MembershipCreateStatus.InvalidEmail
lblResult.ForeColor = Color.Red
lblResult.Text = "The email address you provided is invalid."
Case MembershipCreateStatus.InvalidAnswer
lblResult.ForeColor = Color.Red
lblResult.Text = "The security answer was invalid."
Case MembershipCreateStatus.InvalidPassword
lblResult.ForeColor = Color.Red
lblResult.Text = "The password you provided is invalid. It must be 7 characters long and have at least 1 special character."
Case Else
lblResult.ForeColor = Color.Red
lblResult.Text = "There was an unknown error; the user account was NOT created."
End Select
End Sub
Login
Protected Sub OnAuthenticate(ByVal sender As Object, ByVal e As AuthenticateEventArgs)
If Membership.ValidateUser(Login1.UserName, Login1.Password) Then
FormsAuthentication.RedirectFromLoginPage(Login1.UserName, Login1.RememberMeSet)
Else
Login1.FailureText = "Invalid Username or password."
End If
End Sub
Home
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Me.Page.User.Identity.IsAuthenticated Then
FormsAuthentication.RedirectToLoginPage()
End If
End Sub
Screenshot
