Implement Remember Me Functionality In ASP.NET Web Forms | Learn ASP.NET
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Text;
namespace RememberMeCheckBoxAsp
{
public partial class Login : System.Web.UI.Page
{
string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//To retrieve data from cookie, "Request" class is used
//To send data to cookie, "Response" class is used
if (Request.Cookies["username"] != null)
{
TextBox1.Text = Request.Cookies["username"].Value.ToString();
}
if (Request.Cookies["password"] != null)
{
string EncryptedPassword = Request.Cookies["password"].Value.ToString();
byte[] b = Convert.FromBase64String(EncryptedPassword); //EncryptedPassword will convert from string to byte
string DecryptedPassword = ASCIIEncoding.ASCII.GetString(b);//GetString() method will Decrypt b and also convert "b" from byte to string
TextBox2.Text = DecryptedPassword;
}
if (Request.Cookies["username"] != null && Request.Cookies["password"] != null)
{
CheckBox1.Checked = true;
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(cs);
string query = "select * from Login where username=@user and password=@pass";
SqlDataAdapter sda = new SqlDataAdapter(query,con);
sda.SelectCommand.Parameters.AddWithValue("@user",TextBox1.Text);
sda.SelectCommand.Parameters.AddWithValue("@pass",TextBox2.Text);
DataTable data = new DataTable();
sda.Fill(data);
if (data.Rows.Count > 0)
{
if (CheckBox1.Checked == true)
{
Response.Cookies["username"].Value = TextBox1.Text;
byte[] b = ASCIIEncoding.ASCII.GetBytes(TextBox2.Text);
string EncryptedPassword = Convert.ToBase64String(b);
Response.Cookies["password"].Value = EncryptedPassword;
Response.Cookies["username"].Expires = DateTime.Now.AddDays(2);
Response.Cookies["password"].Expires = DateTime.Now.AddDays(2);
}
else
{
Response.Cookies["username"].Expires = DateTime.Now.AddDays(-2);
Response.Cookies["password"].Expires = DateTime.Now.AddDays(-2);
}
Session["username"] = TextBox1.Text;
Response.Redirect("Dashboard.aspx");
}
else
{
Response.Write("<script>alert('Username or Password is incorrect !!')</script>");
}
}
}
}
Above File is RememberMeCheckBoxAsp\Login.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace RememberMeCheckBoxAsp
{
public partial class Dashboard : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["username"] != null)
{
Response.Write("Welcome " + Session["username"].ToString());
}
else
{
Response.Redirect("Login.aspx");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (Session["username"] != null)
{
Session["username"] = null; //First way to destroy session
//Session.Abandon(); //Second way to destroy session
Response.Redirect("Login.aspx");
}
}
}
}
Above File is RememberMeCheckBoxAsp\Dashboard.aspx.cs
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="RememberMeCheckBoxAsp.Login" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 100%;
}
.auto-style2 {
width: 123px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table cellpadding="4" cellspacing="4" class="auto-style1">
<tr>
<td class="auto-style2">USERNAME</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" Height="27px" Width="246px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">PASSWORD</td>
<td>
<asp:TextBox ID="TextBox2" runat="server" Height="27px" Width="246px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td>
<asp:CheckBox ID="CheckBox1" runat="server" Text="Remember Me" />
</td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td>
<asp:Button ID="Button1" runat="server" Height="35px" OnClick="Button1_Click" Text="LOGIN" Width="91px" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Comments
Post a Comment