.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Melody Anderson

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Using Cookies to Remember Password

Posted By:Baimey Rajesh       Posted Date: December 30, 2012    Points: 200    Category: ASP.NET    URL: http://www.dotnetspark.com  

In this article we will see how to remember user name and password /or remember me functionality in ASP.NET.

In any website you can see an option to remember me on this computer.

One of my clients required the same. I got the solution so thought of sharing with you.

We can do remember password by different techniques.

 Here I have utilized one of the common and easily implementable technique using cookies. Let us see the steps.
Step 1 : Add a Checkbox with Remember me text in the HTML page

<asp:CheckBox ID="CbRememberMe" runat="server"> remember me</asp:CheckBox >
Step 2 : In the Login Button Click function add

if (chkRememberPassword.Checked == true) 
Response.Cookies["UserName"].Value = txtUserName.Text; 
Response.Cookies["PWD"].Value = txtPWD.Text; 
Response.Cookies["UName"].Expires = DateTime.Now.AddMonths(2); 
Response.Cookies["PWD"].Expires = DateTime.Now.AddMonths(2); 
Response.Cookies["UName"].Expires = DateTime.Now.AddMonths(-1); 
Response.Cookies["PWD"].Expires = DateTime.Now.AddMonths(-1); 

Step 3: Paste the following code snippet in page load

if (!IsPostBack) 
if (Request.Cookies["UserName"] != null) 
txtUserName.Text= Request.Cookies["UName"].Value; 
if (Request.Cookies["PWD"] != null) 
txtPWD.Text.Attributes.Add("value", Request.Cookies["PWD"].Value); 
if (Request.Cookies["UserName"] != null && Request.Cookies["PWD"] != null) 
chkRememberPassword.Checked = true; 

This code doesn’t need much change, depends on the requirement.

E.g.: I had a 12 digit card number to remember and I did using String array concept, since I had 12 different boxes to add each digit.

Dim cardNumber As String = Request.Cookies("CardNumber").Value
txtKey1.Text = cardNumber(0)
txtKey2.Text = cardNumber(1)
txtKey3.Text = cardNumber(2)
txtKey4.Text = cardNumber(3)
txtKey5.Text = cardNumber(4)
txtKey6.Text = cardNumber(5)
txtKey7.Text = cardNumber(6)
txtKey8.Text = cardNumber(7)
txtKey9.Text = cardNumber(8)
txtKey10.Text = cardNumber(9)
txtKey11.Text = cardNumber(10)
txtKey12.Text = cardNumber(11)
Hope this helps you

Happy Coding J

 Subscribe to Articles


Further Readings:

Author: Sivaraman         Company URL: http://www.dotnetspark.com
Posted Date: April 15, 2013

Nice share. Thanks Man

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend