Click here to Skip to main content
12,547,922 members (45,184 online)
Rate this:
Please Sign up or sign in to vote.
See more: ASP.NET
Hi, everyone
i Need solution for the following concept in ASP.Net
When the user types username and password with the "Remember ME" checkbox checked it will stored the username and password in the cookie inside the system.
Whenever the user logins again, when username is entered in the textbox the password must be automatically loaded from the cookie. simultaneously the cookie must store multiple username and passwords.

Here is the code for
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CS.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
<script type="text/javascript" language="javascript">
//function writeCookie()
//var txtuname= document.getElementById('txtUserName');
//var txtpass= document.getElementById('txtPassword');

//var exdate=new Date();
//alert( txtuname.value+"="+escape(txtuname.value+"|"+txtpass.value)+";expires="+exdate.toGMTString());
//document.cookie= txtuname.value+"="+escape(txtuname.value+"|"+txtpass.value)+";expires="+exdate.toGMTString();

//alert("Check box is not selected ");


function readCookie()
var txtuname= document.getElementById('txtUserName');
var txtpass= document.getElementById('txtPassword');
if (document.cookie.length>0)
var c_start=document.cookie.indexOf(txtuname.value);
//alert("to search txtvalue "+txtuname.value);

if (c_start!=-1)
c_start=c_start + txtuname.value.length +1 ;
// alert(" start "+c_start);
var c_end=document.cookie.indexOf(";",c_start);
// alert("end "+c_end);
if (c_end==-1)
// alert(c_end);
var value= unescape(document.cookie.substring(c_start,c_end));
// alert("value "+value);
var arr = new Array(2);
arr= value.split('|',2)
alert("Username "+arr[0]);
alert("Password "+arr[1]);
    <form id="form1" runat="server">
    <asp:TextBox ID="txtUserName" runat="server" ></asp:TextBox><br />
    <asp:TextBox ID="txtPassword" TextMode="Password" AutoCompleteType="None" AutoPostBack="false" runat="server"></asp:TextBox><br />
    Remember me:
    <asp:CheckBox ID="chkRememberMe" runat="server" /><br />
    <asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="Login_Click" />
        <asp:Button ID="Button1" runat="server"  Text="Delete" />

Here is the code for CS.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        if (Request.Cookies["UserName"] != null && Request.Cookies["Password"] != null)
                txtUserName.Text = Request.Cookies["UserName"].Value;
                //txtPassword.Attributes["value"] = Request.Cookies["Password"].Value;
                //txtPassword.Text = Request.Cookies["Password"].Value;
                txtPassword.Attributes.Add("OnFocus", "javascript:readCookie();");

    protected void Login_Click(object sender, EventArgs e)
        //btnLogin.Attributes.Add("OnClick", "javascript:writeCookie();");
            if (chkRememberMe.Checked)
                Response.Cookies["UserName"].Value = txtUserName.Text;
                Response.Cookies["Password"].Value = txtPassword.Text;
                Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(30);
                Response.Cookies["Password"].Expires = DateTime.Now.AddDays(30);
                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();
        //protected void Button1_Click(object sender, EventArgs e)
        //    if (Request.Cookies["UserName"] != null && Request.Cookies["Password"] != null)
        //    {
        //        Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(-30);
        //        Response.Cookies["Password"].Expires = DateTime.Now.AddDays(-30);
        //    }
        //    txtUserName.Text = "";
        //    txtPassword.Text = "";

    //protected void txtUserName_TextChanged(object sender, EventArgs e)
    //    txtPassword.Attributes.Add("OnTextChanged", "javascript:readCookie();");
    protected void Button1_Click(object sender, EventArgs e)

Thanks in advance....
Posted 13-Feb-13 1:32am
Updated 13-Feb-13 19:38pm
CodeMaster_Noob 13-Feb-13 7:51am
whats the problem in doin this ??
joe_j 13-Feb-13 7:53am
Please do not use terms like "I need immediate response". People here are just to help and not to provide answers at such demands.
Marcus Kramer 13-Feb-13 12:59pm
There is no question here. You have made a statement of requirements.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Hi Dear if you are using TextBox controls for username and password you can follow this.

protected void btn_Click(object sender, EventArgs e)
            HttpCookie cookie = new HttpCookie("UserName");
            cookie.Values.Add(this.userName.Text, this.password.Text);

here is some javascript code

<pre>function readTheCookie() {
            var the_cookie = document.cookie;
            if (the_cookie != "") {
                document.getElementById('<%=password.ClientID %>').value = the_cookie.split('=')[2];

[Removed begging for vote]

Good Luck

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.161021.1 | Last Updated 9 Dec 2015
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100