Create Login Page Form Example In Asp.Net
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 textboxand 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 textboxand 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#
01
protected
void
btnLogIn_Click(
object
sender, EventArgs e)
02
{
03
//Create Connection String And SQL Statement
04
string
strCon = ConfigurationManager.ConnectionStrings[
"ConnectionString"
].ConnectionString;
05
string
strSelect =
"SELECT COUNT(*) FROM Users WHERE UserName = @Username AND Password = @Password"
;
06
07
SqlConnection con =
new
SqlConnection(strCon);
08
SqlCommand cmd =
new
SqlCommand();
09
cmd.Connection = con;
10
cmd.CommandType = CommandType.Text;
11
cmd.CommandText = strSelect;
12
13
SqlParameter username =
new
SqlParameter(
"@Username"
, SqlDbType.VarChar, 50);
14
username.Value = txtUserName.Text.Trim().ToString();
15
cmd.Parameters.Add(username);
16
17
SqlParameter password =
new
SqlParameter(
"@Password"
, SqlDbType.VarChar, 50);
18
password.Value = txtPwd.Text.Trim().ToString();
19
cmd.Parameters.Add(password);
20
21
con.Open();
22
int
result = (Int32)cmd.ExecuteScalar();
23
con.Close();
24
25
if
(result >= 1)
26
{
27
Response.Redirect(
"UpdateProfile.aspx"
);
28
}
29
else
30
lblMsg.Text =
"Incorrect Username or Password"
;
31
32
}
VB.NET
01
Protected
Sub
btnLogIn_Click(sender
As
Object
, e
As
EventArgs)
02
'Create Connection String And SQL Statement
03
Dim
strCon
As
String
= ConfigurationManager.ConnectionStrings(
"ConnectionString"
).ConnectionString
04
Dim
strSelect
As
String
=
"SELECT COUNT(*) FROM Users WHERE UserName = @Username AND Password = @Password"
05
06
Dim
con
As
New
SqlConnection(strCon)
07
Dim
cmd
As
New
SqlCommand()
08
cmd.Connection = con
09
cmd.CommandType = CommandType.Text
10
cmd.CommandText = strSelect
11
12
Dim
username
As
New
SqlParameter(
"@Username"
, SqlDbType.VarChar, 50)
13
username.Value = txtUserName.Text.Trim().ToString()
14
cmd.Parameters.Add(username)
15
16
Dim
password
As
New
SqlParameter(
"@Password"
, SqlDbType.VarChar, 50)
17
password.Value = txtPwd.Text.Trim().ToString()
18
cmd.Parameters.Add(password)
19
20
con.Open()
21
Dim
result
As
Integer
=
DirectCast
(cmd.ExecuteScalar(), Int32)
22
con.Close()
23
24
If
result >= 1
Then
25
Response.Redirect(
"UpdateProfile.aspx"
)
26
Else
27
lblMsg.Text =
"Incorrect Username or Password"
28
End
If
29
30
End
Sub
We can further use Forms Authentication to anonymous access to site.
No comments:
Post a Comment