Introduction: In
this article I will epxlain how we can remember USERNAME and PASSWORD using
Remember me checkbox during login in asp.net.
Description:
In the last post I have explained How to Create Login Form in Asp.net OR Login Form with Check Username and Password aviabilty in Asp.net, How to create registration page in asp.net and How to Recover Forgot Password via Email in Asp.net.
I created a Table USER_REGISTRATION:
ID
|
int
|
USERNAME
|
varchar(50)
|
PASSWORD
|
varchar(50)
|
FIRST_NAME
|
varchar(50)
|
LAST_NAME
|
varchar(50)
|
SEX
|
varchar(50)
|
EMAIL_ID
|
varchar(50)
|
Here ID is autoincrement and primary key. Registers user
detail/information store in table and check the username and password detlais
on login page.
Add a new webform to project. Drag and drop the control from
Toolbox and desgin the .aspx page as shown below:
<table align="center">
<tr><td>Username:-</td><td>
<asp:TextBox ID="txtusername"
runat="server"></asp:TextBox></td></tr>
<tr><td> </td></tr>
<tr><td>Password:-</td><td>
<asp:TextBox ID="txtpassword"
TextMode="Password"
runat="server"></asp:TextBox></td></tr>
<tr><td> </td></tr>
<tr><td>Remeber me</td><td>
<asp:CheckBox ID="CheckBox1"
runat="server"
/></td></tr>
<tr><td> </td><td>
<asp:Button ID="btnlogin"
runat="server"
Text="Login"
onclick="btnlogin_Click"
/></td></tr>
</table>
Now write the below given code on .aspx.cs page:
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ToString());
protected void
Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.Cookies["USERNAME"]
!= null && Request.Cookies["PASSWORD"] != null)
{
txtusername.Text = Request.Cookies["USERNAME"].Value;
txtpassword.Attributes["value"]
= Request.Cookies["PASSWORD"].Value;
}
}
}
protected void
btnlogin_Click(object sender, EventArgs e)
{
try
{
string query = "select
* from USER_REGISTRATION where USERNAME = @USERNAME and PASSWORD =
@PASSWORD";
SqlCommand cmd = new
SqlCommand(query, con);
cmd.Parameters.AddWithValue("@USERNAME",
txtusername.Text);
cmd.Parameters.AddWithValue("@PASSWORD",
txtpassword.Text);
SqlDataAdapter adp = new
SqlDataAdapter(cmd);
DataTable dt = new
DataTable();
adp.Fill(dt);
if (dt.Rows.Count > 0)
{
if (CheckBox1.Checked)
{
Response.Cookies["USERNAME"].Expires
= DateTime.Now.AddDays(30);
Response.Cookies["PASSWORD"].Expires
= DateTime.Now.AddDays(30);
}
else
{
Response.Cookies["USERNAME"].Expires
= DateTime.Now.AddDays(-1);
Response.Cookies["PASSWORD"].Expires
= DateTime.Now.AddDays(-1);
}
Response.Cookies["USERNAME"].Value
= txtusername.Text.Trim();
Response.Cookies["PASSWORD"].Value
= txtpassword.Text.Trim();
Response.Redirect("Welcome.aspx");
}
else
{
Messagebox("USERNAME/PASSWORD DOES NOT
EXIST");
}
}
catch (Exception
ex)
{
}
}
private void
Messagebox(string Message)
{
Label lblMessageBox = new
Label();
lblMessageBox.Text =
"<script language='javascript'>" +
Environment.NewLine +
"window.alert('" + Message + "')</script>";
Page.Controls.Add(lblMessageBox);
}
In VB (.aspx.vb)
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Private con As New SqlConnection(ConfigurationManager.ConnectionStrings("Connection").ToString())
Protected Sub
btnlogin_Click(sender As Object, e As System.EventArgs) Handles
btnlogin.Click
Try
Dim query As String = "select *
from USER_REGISTRATION where USERNAME = @USERNAME and PASSWORD =
@PASSWORD"
Dim cmd As New SqlCommand(query,
con)
cmd.Parameters.AddWithValue("@USERNAME",
txtusername.Text)
cmd.Parameters.AddWithValue("@PASSWORD",
txtpassword.Text)
Dim adp As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
adp.Fill(dt)
If dt.Rows.Count > 0 Then
If CheckBox1.Checked Then
Response.Cookies("USERNAME").Expires
= DateTime.Now.AddDays(30)
Response.Cookies("PASSWORD").Expires
= DateTime.Now.AddDays(30)
Else
Response.Cookies("USERNAME").Expires
= DateTime.Now.AddDays(-1)
Response.Cookies("PASSWORD").Expires
= DateTime.Now.AddDays(-1)
End If
Response.Cookies("USERNAME").Value
= txtusername.Text.Trim()
Response.Cookies("PASSWORD").Value
= txtpassword.Text.Trim()
Response.Redirect("Welcome.aspx")
Else
Messagebox("USERNAME/PASSWORD DOES NOT
EXIST")
End If
Catch ex As Exception
End Try
End Sub
Protected Sub
Page_Load(sender As Object,
e As System.EventArgs)
Handles Me.Load
If Not IsPostBack Then
If Request.Cookies("USERNAME")
IsNot Nothing AndAlso Request.Cookies("PASSWORD")
IsNot Nothing Then
txtusername.Text = Request.Cookies("USERNAME").Value
txtpassword.Attributes("value")
= Request.Cookies("PASSWORD").Value
End If
End If
End Sub
Private Sub
Messagebox(Message As String)
Dim lblMessageBox As New Label()
lblMessageBox.Text = "<script
language='javascript'>" + Environment.NewLine
& "window.alert('" &
Message & "')</script>"
Page.Controls.Add(lblMessageBox)
End Sub
Run the project and check the result.
No comments:
Post a Comment