Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET login
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
CS.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CS.aspx.cs" Inherits="_Default" %>
 
<!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>
<script type="text/javascript" language="javascript">
 
//function writeCookie()
//{
//var txtuname= document.getElementById('txtUserName');
//var txtpass= document.getElementById('txtPassword');

 
//var exdate=new Date();
//exdate.setDate(exdate.getDate()+15);
//
//if(document.getElementById('chkRememberMe').checked)
//{
//alert( txtuname.value+"="+escape(txtuname.value+"|"+txtpass.value)+";expires="+exdate.toGMTString());
//document.cookie= txtuname.value+"="+escape(txtuname.value+"|"+txtpass.value)+";expires="+exdate.toGMTString();

//}
//else
//{
//alert("Check box is not selected ");

//}
//}

function readCookie()
{
 
var txtuname= document.getElementById('txtUserName');
var txtpass= document.getElementById('txtPassword');
 
debugger
 
if (document.cookie.length>0)
{
var c_start=document.cookie.indexOf(txtuname.value);
//alert(document.cookie);
//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)
{
c_end=document.cookie.length;
// 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]);
txtuname.value=arr[0];
txtpass.value=arr[1];
}
else
{
txtpass.value="";
}
}
 
}
 
</script>
<body>
    <form id="form1" runat="server">
    UserName:
    <asp:TextBox ID="txtUserName" runat="server" ></asp:TextBox><br />
    Password:&nbsp;
    <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" />
    </form>
</body>
</html>
 

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.Add("");
                Response.Redirect("Default.aspx");
            }
            else
            {
                Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(-1);
                Response.Cookies["Password"].Expires = DateTime.Now.AddDays(-1);
                Response.Redirect("Default.aspx");
 
            }
            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
Edited 13-Feb-13 19:38pm
v2
Comments
CodeMaster_Noob at 13-Feb-13 7:51am
   
whats the problem in doin this ??
joe_j at 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 at 13-Feb-13 12:59pm
   
There is no question here. You have made a statement of requirements.
Rate this: bad
good
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)
        {
            //GetValues();
            HttpCookie cookie = new HttpCookie("UserName");
            cookie.Values.Add(this.userName.Text, this.password.Text);
            Response.Cookies.Add(cookie);
        }
 
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]
 
Thanks
Good Luck
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 210
1 Richard MacCutchan 100
2 kbrandwijk 90
3 ProgramFOX 80
4 Sandeep Singh Shekhawat 70
0 Sergey Alexandrovich Kryukov 9,050
1 OriginalGriff 8,151
2 CPallini 2,613
3 Richard MacCutchan 2,221
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 14 Feb 2013
Copyright © CodeProject, 1999-2014
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