Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Remember me password is not working ,help me

C#
protected void btnlogin_Click(object sender, EventArgs e)
        {


            if (rememberme.Checked == true)
            {
                if (Request.Cookies["UserName"] != null)
                {

                    Response.Cookies["USERNAME"].Value = log.Text;
                    Response.Cookies["USERNAME"].Expires = DateTime.Now.AddYears(30);
                    Response.Cookies["PASSWORD"].Value = pwd.Text;
                    Response.Cookies["PASSWORD"].Expires = DateTime.Now.AddYears(30);
                }


            }
            else
            {
                Response.Cookies["USERNAME"].Expires = DateTime.Now.AddYears(-30);
                Response.Cookies["PASSWORD"].Expires = DateTime.Now.AddYears(-30);
            }

        }
Posted
Updated 3-Jan-12 1:33am
v2
Comments
Smithers-Jones 3-Jan-12 7:34am    
"It's not working" - That's what a five years old would say. A developer would describe, what exactly is not working, what error message he gets, where the problem lies. Are you five years old?

Added code-block.
bbirajdar 3-Jan-12 8:13am    
Nice comment Smithers..
bbirajdar 3-Jan-12 8:13am    
@priya.. check if cookies are enabled in your browser.

Make sure cookies are turned on and that you can write and read back a simple test cookie across sessions. The problem is almost certainly related to that.

Have a look at the request you're actually sending from the browser, and the response it receives, and see if the cookies are being set correctly. Do they have the correct path?
 
Share this answer
 
import System.Net namespace in your code.

Add the following code to click event of your login button:


C#
if (chkRemember.Checked)
{
HttpCookie cookie = new HttpCookie("YourAppLogin");
cookie.Values.Add("username", txtUsername.Text);
cookie.Expires = DateTime.Now.AddDays(15);
Response.Cookies.Add(cookie);
}


In the code above first we check if user has checked "Remember Me"
checkbox, if its checked then we create a HTTPCookie and we set an expiry date for it.

Now you should add this code to every page that requires login :

C#
if (Request.Cookies["YourApLogin&"] != null)
{
string username = Request.Cookies["YourAppLogin"].Values["username"]);
}


This code checks the cookie and returns Username.
 
Share this answer
 
Comments
priya from Madras 5-Jan-12 0:31am    
hi bala thanks for ur reply but where should i place the above code in my code
Balakrishnan Dhinakaran 6-Jan-12 0:20am    
Hello Priya place this inside button click event
C#
protected void Page_Load(object sender, EventArgs e)
       {
           if (!IsPostBack)
           {
               

               if (Request.Cookies["USERNAME"] != null)
                   log.Text = Request.Cookies["USERNAME"].Value;
               if (Request.Cookies["PASSWORD"] != null)
                   pwd.Text = Request.Cookies["PASSWORD"].Value;
                   pwd.Attributes.Add("value", Request.Cookies["PASSWORD"].Value);

               //txtpwd.Attributes.Add("values", Request.Cookies["PASSWORD"].Value);
               if (Request.Cookies["USERNAME"] != null && Request.Cookies["PASSWORD"] != null)
                   rememberme.Checked = true;
              
           }
        }



in button click event like this


C#
if (rememberme.Checked == true)
            {

                Response.Cookies["USERNAME"].Value = log.Text;
                Response.Cookies["USERNAME"].Expires = DateTime.Now.AddYears(30);
                Response.Cookies["PASSWORD"].Value = pwd.Text;
                Response.Cookies["PASSWORD"].Expires = DateTime.Now.AddYears(30);

                //HttpCookie cookie = new HttpCookie();

            }
            else
            {
                Response.Cookies["USERNAME"].Expires = DateTime.Now.AddYears(-30);
                Response.Cookies["PASSWORD"].Expires = DateTime.Now.AddYears(-30);
            }


but it has some problem , help me
 
Share this answer
 
v2
Comments
Smithers-Jones 4-Jan-12 3:08am    
"but it has some problem" - Really? Let me guess: it's not working. Seriously, as I pointed out in my last comment, describe the problem, don't just say "not working/has a problem". How would anybody know, what that problem is???

Added code-block. Oh, and not an answer.
priya from Madras 5-Jan-12 0:29am    
oh sorry smith actually when i give username and enter the tab key the password is automatically comes to the textbox , if remember me check box condition is checked ,incase remember checkbox is not checked password never come , so user needs to enter their password in textbox , but this logic is not working in my above code .

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


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