Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
1.00/5 (6 votes)
See more:
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
XML
<%@ 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

C#
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
Updated 13-Feb-13 19:38pm
v2
Comments
[no name] 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.
fjdiewornncalwe 13-Feb-13 12:59pm    
There is no question here. You have made a statement of requirements.

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


C#
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

JavaScript
<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
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900