This example shows how to Create Login Page Form In Asp.Net Using C# And VB.NET.
To create Login Page Example, i have added a new form and placed 2 textbox and one button on it.
RequiredFieldValidators are associated to respective textboxes to ensure they are not blank.
TextMode property of password textbox is set so that it displays * instead of letters.
We can also Login Control with membership provider database to create login page.
I have used a table called users to check username and password, schema is shown below.
You can use User Registration Form In Sign Up hyperlink and Forgot Password Page to retrieve password by email.
HTML SOURCE OF PAGE
Write following code in Click Event of Log In Button.
C#
VB.NET
We can further use Forms Authentication to anonymous access to site.
To create Login Page Example, i have added a new form and placed 2 textbox and one button on it.
RequiredFieldValidators are associated to respective textboxes to ensure they are not blank.
TextMode property of password textbox is set so that it displays * instead of letters.
We can also Login Control with membership provider database to create login page.
I have used a table called users to check username and password, schema is shown below.
You can use User Registration Form In Sign Up hyperlink and Forgot Password Page to retrieve password by email.
HTML SOURCE OF PAGE
<form id="Form1" runat="server">
<fieldset>
<legend>Login</legend>
<div class='container'>
<asp:Label ID="Name" runat="server" Text="UserName:" CssClass="lbl"/>
<br/>
<asp:TextBox ID="txtUserName" runat="server" Height="22px"/>
<asp:RequiredFieldValidator ID="RV1" runat="server"
ControlToValidate="txtUserName"
ErrorMessage="Please Enter User Name"
SetFocusOnError="True">*
</asp:RequiredFieldValidator><br />
</div>
<div class='container'>
<asp:Label ID="lblPwd" runat="server" Text="Password:" CssClass="lbl"/>
<br/>
<asp:TextBox ID="txtPwd" runat="server" TextMode="Password"
CssClass="pwd" Height="22px"/>
<asp:RequiredFieldValidator ID="RV2" runat="server"
ControlToValidate="txtPwd"
ErrorMessage="Your Password"
SetFocusOnError="True">*
</asp:RequiredFieldValidator><br />
</div>
<div class='container'>
<asp:Button ID="btnLogIn" runat="server" Text="Sign In"
onclick="btnLogIn_Click"/>
</div>
<div class='container'>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl="~/ForgotPassword.aspx">Forgot Password ?</asp:HyperLink>
<br/>
</div>
<div class='short_explanation'>New User ?
<asp:HyperLink ID="HyperLink2" runat="server"
NavigateUrl="~/Default.aspx">SignUp !</asp:HyperLink></div>
<asp:ValidationSummary ID="ValidationSummary1"
runat="server" CssClass="error"/>
<br /><br />
<asp:Label ID="lblMsg" runat="server" Text="" CssClass="lbl"/>
</fieldset>
</form>
Write following code in Click Event of Log In Button.
C#
protected void btnLogIn_Click(object sender, EventArgs e) { //Create Connection String And SQL Statement string strCon = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; string strSelect = "SELECT COUNT(*) FROM Users WHERE UserName = @Username AND Password = @Password"; SqlConnection con = new SqlConnection(strCon); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandType = CommandType.Text; cmd.CommandText = strSelect; SqlParameter username = new SqlParameter("@Username", SqlDbType.VarChar, 50); username.Value = txtUserName.Text.Trim().ToString(); cmd.Parameters.Add(username); SqlParameter password = new SqlParameter("@Password", SqlDbType.VarChar, 50); password.Value = txtPwd.Text.Trim().ToString(); cmd.Parameters.Add(password); con.Open(); int result = (Int32)cmd.ExecuteScalar(); con.Close(); if (result >= 1) { Response.Redirect("UpdateProfile.aspx"); } else lblMsg.Text = "Incorrect Username or Password"; }
VB.NET
Protected Sub btnLogIn_Click(sender As Object, e As EventArgs) 'Create Connection String And SQL Statement Dim strCon As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString Dim strSelect As String = "SELECT COUNT(*) FROM Users WHERE UserName = @Username AND Password = @Password" Dim con As New SqlConnection(strCon) Dim cmd As New SqlCommand() cmd.Connection = con cmd.CommandType = CommandType.Text cmd.CommandText = strSelect Dim username As New SqlParameter("@Username", SqlDbType.VarChar, 50) username.Value = txtUserName.Text.Trim().ToString() cmd.Parameters.Add(username) Dim password As New SqlParameter("@Password", SqlDbType.VarChar, 50) password.Value = txtPwd.Text.Trim().ToString() cmd.Parameters.Add(password) con.Open() Dim result As Integer = DirectCast(cmd.ExecuteScalar(), Int32) con.Close() If result >= 1 Then Response.Redirect("UpdateProfile.aspx") Else lblMsg.Text = "Incorrect Username or Password" End If End Sub
We can further use Forms Authentication to anonymous access to site.
If you like this post than join us or share
8 comments:
Nice Article Please Provide Sample Code to download
Please add the download file.
please add the css file also
I really appreciate what you post. You have a new subscriber now.
its an simplest form of log in code.. i need complex form of log in code...
thanks. it helped me a lot. :)
thank you! :D
Thank q...
Post a Comment