How to make the remember me checkbox in login page in asp.net c#

Many time , we have seen the sign or remember me in login page on gmail, yahoo , Hotmail, facebook and so no. in this article , I will  solve this problem , I am giving  you a code for that through this code we can the save or remember me the username or password always for the particular username .

Design:-




Sql Server :-
Deign of table

Data of table


Source code :-
<%@ 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>neerajcodesolutions_rememberme</title>
    <style type="text/css">
        .style1
        {
            font-size: medium;
            font-weight: bold;
        }
        .style2
        {
            font-size: medium;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <fieldset style="width: 250px;">
            <legend><strong>Email/password remember me</strong> </legend>
            <table>
                <tr>
                    <td class="style2">
                        <strong><span class="style2">Email</span></strong><span class="style1">:-
                    </span>
                    </td>
                    <td>
                        <asp:TextBox ID="txtemail" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="style2">
                        <strong><span class="style2">Password</span></strong><span class="style1">:-
                        </span>
                    </td>
                    <td>
                        <asp:TextBox ID="txtpwd" TextMode="Password" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="style2">
                        <strong><span class="style2">Remeber me</span></strong><span class="style1">
                        </span>
                    </td>
                    <td>
                        <asp:CheckBox ID="chkremember" runat="server" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Button ID="btnlogin" runat="server" Text="Login" OnClick="btnlogin_Click"
                            Width="90px" />
                    </td>

                </tr>
            </table>
        </fieldset>
    </div>
    </form>
</body>
</html>


Code behind (c#):-
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.Data;
using System.Configuration;

public partial class rememberme : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CodeSolutionConnectionString"].ToString());
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            if (Request.Cookies["email"] != null && Request.Cookies["password"] != null)
            {
                txtemail.Text = Request.Cookies["email"].Value;
                txtpwd.Attributes["value"] = Request.Cookies["password"].Value;
            }
        }
    }

    protected void btnlogin_Click(object sender, EventArgs e)
    {
        try
        {

            string sqlquery = "select email,password from logintable where email = @email and password = @password";

            SqlCommand cmd = new SqlCommand(sqlquery, con);

            cmd.Parameters.AddWithValue("@email", txtemail.Text);

            cmd.Parameters.AddWithValue("@password", txtpwd.Text);

            SqlDataAdapter adp = new SqlDataAdapter(cmd);

            DataTable dt = new DataTable();

            adp.Fill(dt);

            if (dt.Rows.Count > 0)
            {

                if (chkremember.Checked)
                {
                    Response.Cookies["email"].Expires = DateTime.Now.AddDays(30);
                    Response.Cookies["password"].Expires = DateTime.Now.AddDays(30);
                }
                else
                {
                    Response.Cookies["email"].Expires = DateTime.Now.AddDays(-1);
                    Response.Cookies["password"].Expires = DateTime.Now.AddDays(-1);
                }

                Response.Cookies["email"].Value = txtemail.Text.Trim();
                Response.Cookies["password"].Value = txtpwd.Text.Trim();
                Response.Redirect("https://www.neerajcodesolutions.blogspot.in");
            }

            else
            {

                Response.Write("Please enter correct email/password");
            }

        }
        catch (Exception ex)
        {
           

        }
    }
}

Output:-



How to make the remember me checkbox in login page in asp.net c# How to make the remember me checkbox in login page in asp.net c# Reviewed by NEERAJ SRIVASTAVA on 9:58:00 PM Rating: 5

1 comment:

  1. bro its not working ..
    there was no error in code the only problem was remember me feature was not working

    ReplyDelete

Powered by Blogger.