Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create persistent cookies in ASP.NET?

I am creating cookies with following lines:

HttpCookie userid = new HttpCookie("userid", objUser.id.ToString()); userid.Expires.AddYears(1); Response.Cookies.Add(userid); 

Now how can I make it persistent?

If I visit the same page again after closing the browser, I'm unable to get it back.

like image 214
Vikas Avatar asked Jun 29 '10 11:06

Vikas


People also ask

What is persistent cookie in asp net?

Persistent Cookies: Persistent Cookies are Permanent Cookies stored as a text file in the hard disk of the computer. Non-Persistent Cookies: Non-Persistent cookies are temporary. They are also called in-memory cookies and session-based cookies.

How are cookies persistent?

Persistent cookies are stored on a user's device to hold usage information, settings, personalizations, or sign-on credentials. The primary purpose of persistent cookies is to provide users with an immersive and seamless browsing experience by auto-filling information that the user may have already provided.

Is it necessary to write expiry date in cookies in asp net?

If a cookie is valid and can be read by the domain, it will be passed along with the HTTP request to the domain that it originated from. If you want a cookie to expire at a specific time, you need to set an expiration date.


2 Answers

Here's how you can do that.

Writing the persistent cookie.

//create a cookie HttpCookie myCookie = new HttpCookie("myCookie");  //Add key-values in the cookie myCookie.Values.Add("userid", objUser.id.ToString());  //set cookie expiry date-time. Made it to last for next 12 hours. myCookie.Expires = DateTime.Now.AddHours(12);  //Most important, write the cookie to client. Response.Cookies.Add(myCookie); 

Reading the persistent cookie.

//Assuming user comes back after several hours. several < 12. //Read the cookie from Request. HttpCookie myCookie = Request.Cookies["myCookie"]; if (myCookie == null) {     //No cookie found or cookie expired.     //Handle the situation here, Redirect the user or simply return; }  //ok - cookie is found. //Gracefully check if the cookie has the key-value as expected. if (!string.IsNullOrEmpty(myCookie.Values["userid"])) {     string userId = myCookie.Values["userid"].ToString();     //Yes userId is found. Mission accomplished. } 
like image 101
this. __curious_geek Avatar answered Sep 25 '22 16:09

this. __curious_geek


Although the accepted answer is correct, it does not state why the original code failed to work.

Bad code from your question:

HttpCookie userid = new HttpCookie("userid", objUser.id.ToString()); userid.Expires.AddYears(1); Response.Cookies.Add(userid); 

Take a look at the second line. The basis for expiration is on the Expires property which contains the default of 1/1/0001. The above code is evaluating to 1/1/0002. Furthermore the evaluation is not being saved back to the property. Instead the Expires property should be set with the basis on the current date.

Corrected code:

HttpCookie userid = new HttpCookie("userid", objUser.id.ToString()); userid.Expires = DateTime.Now.AddYears(1); Response.Cookies.Add(userid); 
like image 33
Chance Avatar answered Sep 24 '22 16:09

Chance