Monday, May 23, 2016

Remember me on checkbox checked on login page in asp.net

In this article, I am going to explain how to implement  "Remember Me" functionality for next time in ASP. net c#.

Copy the aspx code in your aspx page.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RememberMe.aspx.cs" Inherits="RememberMe" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
   
    <fieldset>
    <legend>User Login</legend>
    <table>
    <tr>
    <td>User Name: </td>
    <td><asp:TextBox ID="txtUserName" runat="server"></asp:TextBox><span style="color:Red">*</span>
    <asp:RequiredFieldValidator ID="rfvtxtUserName" ValidationGroup="M" Display="None"
    ControlToValidate="txtUserName" runat="server" ErrorMessage="Please enter User Name"></asp:RequiredFieldValidator>
    </td>
    </tr>
    <tr>
    <td>Password: </td>
    <td><asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox><span style="color:Red">*</span>
     <asp:RequiredFieldValidator ID="rfvtxtPassword" ValidationGroup="M" Display="None"
    ControlToValidate="txtPassword" runat="server" ErrorMessage="Please enter Password"></asp:RequiredFieldValidator>
    </td>
    </tr>
    <tr>
    <td><asp:Button ID="btnLogin" runat="server" Text="Login" onclick="btnLogin_Click" ValidationGroup="M"/>
    <asp:CheckBox ID="chkNextTime" runat="server" Text="Remember Me Next Time" />
    </td>
    </tr>
 
    </table>
    </fieldset>
    <asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="M"
        ShowMessageBox="true" ShowSummary="false" HeaderText="following error occurs"
        ForeColor="Red" Font-Bold="true" />
    </form>
</body>
</html>

Now,Copy the aspx,cs code in your cs page.

using System;
 
public partial class RememberMe : System.Web.UI.Page
{
    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)
    {
        if (chkNextTime.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();
    }
}


OutPut:


No comments:

Post a Comment