Implement Remember Me Functionality In ASP.NET Web Forms | Learn ASP.NET

 <%@ 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: 106px;

        }

    </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" Width="188px"></asp:TextBox>

                    </td>

                </tr>

                <tr>

                    <td class="auto-style2">PASSWORD</td>

                    <td>

                        <asp:TextBox ID="TextBox2" runat="server" Width="188px"></asp:TextBox>

                    </td>

                </tr>

                <tr>

                    <td class="auto-style2">&nbsp;</td>

                    <td>

                        <asp:CheckBox ID="CheckBox1" runat="server" Text="Remember Me"/>

                    </td>

                </tr>

                <tr>

                    <td class="auto-style2">&nbsp;</td>

                    <td>

                        <asp:Button ID="Button1" runat="server" Text="LOGIN" Height="32px" Width="83px" OnClick="Button1_Click"/>

                    </td>

                </tr>

            </table>

        </div>

    </form>

</body>

</html>

ABOVE FILE IS Login.aspx















using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
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)
            {
                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);
                    string DecryptedPassword = ASCIIEncoding.ASCII.GetString(b);

                    TextBox2.Text = DecryptedPassword;
                }
                if (Request.Cookies["username"] != null && Request.Cookies["password"] != null)
                {
                    CheckBox1.Checked = true;
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                using(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>");
                    }
                }
            }
            catch (SqlException ex)
            {
                Response.Write("SqlException: " + ex.Message);
            }
            catch (Exception ex)
            {
                Response.Write("Exception: " + ex.Message);
            }
        }
    }
}
ABOVE FILE IS Login.aspx.cs















<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Dashboard.aspx.cs" Inherits="RememberMeCheckBoxAsp.Dashboard" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" Text="LOGOUT" Height="37px" OnClick="Button1_Click" Width="101px"/>
        </div>
    </form>
</body>
</html>
ABOVE FILE IS Dashboard.aspx















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;
                Session.Abandon();//Current Session will be deleted
                Response.Redirect("Login.aspx");
            }
        }
    }
}
ABOVE FILE IS Dashboard.aspx.cs






Comments

Popular posts from this blog

Range Validator Control In ASP.NET Web forms | Form Validation | ASP.NET Web forms

Query String In ASP.NET Web Forms | State Management | ASP.NET | Web Forms

Validation Summary Control In ASP.NET Web forms | Form Validation | ASP.NET