Click here to Skip to main content
16,012,468 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi folk's

I have a login form in that if i check the remember me check-box,then i want to remember username and password ..

below code is for remember me.. functionality...it is working fine but only one problem ...After checking remember me check box.. if i reload login page the checked remember not remains can any one help me how can i resolve this problem.....


Can any one help me......
My code is in this way..........for remember me .....

Below code is working fine only problem is checked remember is not remains ....
protected void Page_Load(object sender, EventArgs e)
   {
       if (Page.IsPostBack)
       {
           if (chk_rem.Checked)
           {
               HttpCookie mycookie1 = new HttpCookie("mycookie1");
               //Response.Cookies.Remove("mycookie1");

               Response.Cookies.Add(mycookie1);

               mycookie1.Values.Add("Username", txt_username.Text);
               mycookie1.Values.Add("Password", txt_password.Text);
               DateTime dtex = DateTime.Now.AddDays(15);

               if (chk_rem.Checked == true)
               {
                   if (Request.Cookies["mycookie1"] != null)
                   {
                       HttpCookie getcookie1 = Request.Cookies.Get("mycookie1");
                       string un = getcookie1.Values["Username"].ToString();
                       string ps = getcookie1.Values["Password"].ToString();
                   }

               }

           }
           if (chk_rem.Checked == false)
           {
               Response.Cookies["mycookie1"].Expires = DateTime.Now.AddDays(-1);
           }
       }


       if (HttpContext.Current.Request.Cookies["mycookie1"] != null)
       {
           try
           {
               txt_username.Text = HttpContext.Current.Request.Cookies["mycookie1"]["Username"].ToString();
               string a = HttpContext.Current.Request.Cookies["mycookie1"]["Password"].ToString();
               txt_password.Attributes.Add("value", a);

               chk_rem.Checked = true;
           }
           catch { }
       }

   }

Thank's&Regard's
mahesh.b.p.c
Posted
Updated 2-Sep-12 23:36pm
v2
Comments
L Viljoen 3-Sep-12 6:23am    
What you are currently doing poses as a massive security risk as the username and password is being stored in clear text on a cookie.

What i recommend is generating a user id (GUID) and encrypting it in order to automatically log a user in instead of saveing the login details on the client pc., also what you are doing is currently begin done automatically by most mainstream browsers.

1 solution

Hi,

You are storing cookies for checked Remember me button. So you need to add one condition after first if condition. you can check if cookies exist then you need to manually check that checkbox. You need to do that because control never persist it's value after next postback(refresh page).

And even if credential is stored then you do not need to display that page. just redirect to user's home screen. At the time user click logout, you can remove cookies and user can enter username & password(this will depend upon your requirement).

C#
if (Page.IsPostBack)
        {
            if (Response.Cookies["mycookie1"] != null)
            {
                chk_rem.Checked = true;
            }
            if (chk_rem.Checked)
            {
                HttpCookie mycookie1 = new HttpCookie("mycookie1");
                //Response.Cookies.Remove("mycookie1");
               
                Response.Cookies.Add(mycookie1);
               
                mycookie1.Values.Add("Username", txt_username.Text);
                mycookie1.Values.Add("Password", txt_password.Text);
                DateTime dtex = DateTime.Now.AddDays(15);
 
                if (chk_rem.Checked == true)
                {
                    if (Request.Cookies["mycookie1"] != null)
                    {
                        HttpCookie getcookie1 = Request.Cookies.Get("mycookie1");
                        txtUserName.Text = getcookie1.Values["Username"].ToString();
                        txtPassword.Password = getcookie1.Values["Password"].ToString();
                    }
 
                }
 
            }
            if (chk_rem.Checked == false)
            {
                Response.Cookies["mycookie1"].Expires = DateTime.Now.AddDays(-1);
            }
        }

Hope you got the idea from above statement,
Thanks
-Amit Gajjar
 
Share this answer
 
v2
Comments
mahesh.b.p.c 3-Sep-12 5:48am    
i want if i checked the check box ,and i closed the page and again i want to store the value in the controls, and checked check box must be there...
AmitGajjar 3-Sep-12 5:50am    
in that case ,

txtUserName.Text = getcookie1.Values["Username"].ToString();
txtPassword.Password = getcookie1.Values["Password"].ToString();

Instead of storing un and ps string value you need to set like above.
mahesh.b.p.c 3-Sep-12 5:59am    
even though still not working! my requirement is in this way once checked the remember me and i login and i close the page ..and again i run the page i want the remember me checked and values in the textbox...
AmitGajjar 3-Sep-12 6:04am    
Please check my improved solution.
AmitGajjar 3-Sep-12 6:04am    
you also need to remove last if condition instead you need to write there else statement.

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