JavaScript - Remember me checkbox

Asked By santhosh on 13-Apr-10 06:27 AM
Hi,

I want a remember me check box code in javascript.

When the username and pasword is keyed in and he checks remember me check box and when he clicks submit button , a cookie should be stored.

When the user visits the site again . The user name and passwd should be populated and the check box should be checked.

When the user unchecks the checkbox and clicks the submit button with username nad password. The cookie which was already avl should not removed and on further visits the check box and uname and pwd textbox should be empty.

Kindly help.

Thanks in Advance ,
Santhosh
Web Star replied to santhosh on 13-Apr-10 06:40 AM
use this code work for store user credential for feature use
Put 2 TextBoxes,labels for user & Password,1 submit button & also put 1 check box with caption "remember me in this computer".
//On the page load add this code
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack )
{
if (Request.Cookies["USERNAME"] != null)
txtusername.Text=Request.Cookies["USERNAME"].Value;
if (Request.Cookies["PASSWORD"] != null)
txtpassword.Attributes.Add("value", Request.Cookies["PASSWORD"].Value);
if (Request.Cookies["USERNAME"] != null && Request.Cookies["PASSWORD"] != null)
CheckBox1.Checked=true;
}
}
 
//Add this code on to the button click
private void Button1_Click(object sender, System.EventArgs e)
{
//Da Add Code here to check user exist in the database using reader,sqlcommand etc....
//if reader has rows
if(CheckBox1.Checked==true)
{
Response.Cookies["USERNAME"].Value = txtusername .Text ;
Response.Cookies["USERNAME"].Expires = DateTime.Now.AddMonths(1);
Response.Cookies["PASSWORD"].Value = txtpassword .Text ;
Response.Cookies["PASSWORD"].Expires = DateTime.Now.AddMonths(1);
}
else
{
Response.Cookies["USERNAME"].Expires = DateTime.Now.AddMonths(-1);
Response.Cookies["PASSWORD"].Expires = DateTime.Now.AddMonths(-1);
}
Response.Redirect("");
//
//else Add Code here to validate.
Anand Malli replied to santhosh on 13-Apr-10 06:42 AM
hi santosh

answer lies in your question only,you are right it can be done by cookie document.cookie does the game i am sending you some code with which you will be able to create and read cookies as well as delete

function createCookie(userName,value,days) 
{
   if (days) 
   {
      var date = new Date();
      date.setTime(date.getTime()+(days*24*60*60*1000));
      var expires = "; expires="+date.toGMTString();
   }
   else 
      var expires = "";
  
   document.cookie = userName+"="+value+expires+"; path=/";
}
  
function readCookie(userName) 
{
   var nameEQ = userName + "=";
   var ca = document.cookie.split(';');
   for(var i=0;i < ca.length;i++) 
   {
      var c = ca[i];
      while (c.charAt(0)==' ') 
          c = c.substring(1,c.length);
      if (c.indexOf(nameEQ) == 0) 
         return c.substring(nameEQ.length,c.length);
   }
   return null;
}
function deleteCookie(userName) {
    deleteCookie(userName,"",-1);
}


so from your login button you will call createCookie() function with required argument, in your case the value of your user name textbox

and you can use it further and customise as per your need

thxs
nes aschilles replied to santhosh on 13-Apr-10 06:49 AM
I shouldn't use cookie by javascript, instead you can use HttpCookie object, like follow:
- when click event of login, you save info to cookie:
if (users.Login(userName, passWord))
            {
             HttpCookie cookie = new HttpCookie("PortalUser");
                if (RememberMe.Checked)
                {
                  
                    cookie.Values["AC"] =userName;
                    cookie.Values["PW"] = Encrypt(userName, passWord);// you change your encrypt method is here
                    
                }else{
 
                    cookie.Values["AC"] ="";
                   cookie.Values["PW"] = "";
              }
cookie.Values["Remember"] = RememberMe.Checked.ToString();/
                    cookie.Expires = DateTime.Now.AddDays(10);
                    Response.Cookies.Add(cookie);
                FormsAuthentication.SetAuthCookie(userName, false);
                  
                Response.Redirect("Default.aspx");
 
            }



- And load event of login page:

HttpCookie cookie = (HttpCookie)Request.Cookies["PortalUser"];
                if (cookie != null)
                {                  
 
                    string userName=cookie.Values["AC"];
                    string passWord = DeEncryted(userName, cookie.Values["PW"]);
                    bool remember = bool.Parse(cookie.Values["Remember"]);
                    txtUserName.Text = userName;
                    txtPassWord.Text = passWord;
                    RememberMe.Checked = remember;                     
                     
                }

Endly, I think when click remember me, your system don't show Login page, it login automatically and redirect to Default.aspx directly. It's useful with your client, in my experience.

And load event of login page:

HttpCookie cookie = (HttpCookie)Request.Cookies["PortalUser"];
                if (cookie != null)
                {                 
  
                    string userName=cookie.Values["AC"];
                    string passWord = DeEncryted(userName, cookie.Values["PW"]);
                    bool remember = bool.Parse(cookie.Values["Remember"]);
                    txtUserName.Text = userName;
                    txtPassWord.Text = passWord;
                    RememberMe.Checked = remember;                    
                      
                }
                if (users.Login(userName, passWord))
                {                     
                     Response.Redirect("Default.aspx");
                }



scott replied to Web Star on 24-Nov-10 10:25 AM
Niceness!!