Saturday 7 November 2015

Implement Remember Me functionality using CheckBox ASP.Net


remember me login functionality



Description:-

In this example we explain that how to Implements remember me checkbox functionality in login page in asp.net using C#.

We all know that in every social website there is a facility for the remember me checkbox when we are login at this website. Here we create same functionality when user login, here we simply uses the cookie to store the username and password for particular user for specific time if user check mark the remember me checkbox when login. When user redirect to login page second time at that time we create code that will check the if cookie for username and password is exists then it will automatically set the username and password from the cookie in page load and user do not have to type credential for every time when they want to login to website.


So how to implement the remember me checkbox functionality in our website using cookie in asp.net.

RememberMeForm.aspx:-


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

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Implement Remember Me functionality using CheckBox ASP.Net</title>
</head>
<body>
    <form id="frmRememberMe" runat="server">
        UserName:
    <asp:TextBox ID="txtUnm" runat="server"></asp:TextBox><br />
        Password:
    <asp:TextBox ID="txtPwd" TextMode="Password" runat="server"></asp:TextBox><br />
        Remember me:
    <asp:CheckBox ID="chkbxRememberMe" runat="server" /><br />
        <asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="Login_Click" />
    </form>
</body>
</html>

RememberMeForm.aspx.cs:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class RememberMeForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Cookies["UserName"] != null && Request.Cookies["Password"] != null)
                {
                    txtUnm.Text = Request.Cookies["UserName"].Value;
                    txtPwd.Attributes["value"] = Request.Cookies["Password"].Value;
                }
            }
        }
        protected void Login_Click(object sender, EventArgs e)
        {
            if (chkbxRememberMe.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 = txtUnm.Text.Trim();
            Response.Cookies["Password"].Value = txtPwd.Text.Trim();
        }
    }
}

0 comments:

Post a Comment