Click here to Skip to main content
14,980,370 members
Articles / Web Development / ASP.NET
Tip/Trick
Posted 28 Jan 2010

Tagged as

Stats

68.7K views
7 bookmarked

Implement Remember me on Login Page

Rate me:
Please Sign up or sign in to vote.
2.71/5 (11 votes)
28 Jan 2010CPOL
Most of website have a "Remember Me" checkbox on their website login page and you do not have to login every time you visit that website. Bellow is a simple implementation for this task: First of all you should know that this is a Cookie based solution and its not safe to store important...
Most of website have a "Remember Me" checkbox on their website login page and you do not have to login every time you visit that website.
Bellow is a simple implementation for this task:
First of all you should know that this is a Cookie based solution and its not safe to store important data such ass password in cookies.
now lets get to the code.
First of all import System.Net namespace in your code.
Then add a checkbox control in your login form and name it chkRemember.
then add the following code to click event of your login button:
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.

License

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

Share

About the Author

arash2009
Australia Australia
No Biography provided

Comments and Discussions

 
QuestionHow to Set data getting from request Pin
MayurPadhiyar28-Apr-16 2:17
MemberMayurPadhiyar28-Apr-16 2:17 
QuestionHow can i clear this cookies on log out ? Pin
Am Gayathri13-Apr-15 1:42
MemberAm Gayathri13-Apr-15 1:42 
BugNot Working... Pin
rajdeep2962961-Nov-14 22:57
Memberrajdeep2962961-Nov-14 22:57 
GeneralWhat do you mean by ("YourAppLogin")? Reply as soon as possi... Pin
a1sujan17-May-11 5:57
Membera1sujan17-May-11 5:57 
GeneralRe: it is the value or can we say it as a reference name of http... Pin
d.bonee25-May-11 19:43
Memberd.bonee25-May-11 19:43 
GeneralVulnerable! Pin
SalarSoft28-Jan-10 7:56
MemberSalarSoft28-Jan-10 7:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.