How to create a persistent and non-persistent cookie?

I cannot figure out how to create a persistent and non-persistent cookie. How do they differ, say, in the HTTP headers that are sent back?

+46
cookies
06 Oct '10 at 5:57
source share
3 answers

Cookies have an expiration date implicitly or explicitly determine which controls are stored for a long time (provided that the user agent actually uses them). A cookie can only be stored for the entire session (or even a shorter period).

If the cookie is valid, it will be transmitted along with the HTTP request to the domain from which it originated. Only the domain that sets the cookie can read the cookie (although there are ways to use it, such as cross-site scripting).

  • If you want the cookie to expire at a specific time, set the expiration date on it using the language of the client or server of your choice.

  • If you want the cookie to expire at the end of the session, do not set an expiration date.

From RFC (emphasis mine):

The cookie installer may indicate the date of removal, in which case the cookie will be deleted on that date.

If the cookie installer does not indicate a date, the cookie is deleted after the user logs out of their browser.

As a result, specifying a date is a way of making cookies survive sessions. For this reason, cookies with an expiration date are persistent.

As an example of an application, a shopping site may use persistent cookies to store goods that users have placed in their basket. (In fact, a cookie can refer to an entry in the database stored on the trading site, and not on your computer.) Thus, if users leave their browser without making a purchase and return later, they still find the same items in the trash. therefore, they do not need to search for these items again. If these cookies were not given an expiration date, they expire when the browser is closed and information about the contents of the basket will be lost.

+64
Oct 06 '10 at 6:21
source share

There are two types of cookies in ASP.NET

Permanent cookies:

Cookies are stored on your computer’s hard drive. They remain on your hard drive and can be accessed by web servers until they are deleted or expired.

public void SetPersistentCookies(string name, string value) { HttpCookie cookie = new HttpCookie(name); cookie.Value = value; cookie.Expires = Convert.ToDateTime("12/12/2008β€³); Response.Cookies.Add(cookie); } 

Continuous Cookies:

Cookies are only saved while your web browser is running. They can only be used by the web server until the browser closes. They are not saved on your disk.

 public void SetNonPersistentCookies(string name, string value) { HttpCookie cookie = new HttpCookie(name); cookie.Value = value; Response.Cookies.Add(cookie); } 
+8
Aug 02 2018-11-11T00:
source share

Cookie session

 HttpCookie cookie = new HttpCookie("myCookieName", "myCookieValue"); Response.Cookies.Add(cookie); 

Timestamped cookie ( .NET DateTime library )

 HttpCookie cookie = new HttpCookie("myCookieName", "myCookieValue"); cookie.Expires = DateTime.Today.AddMonths(12); //or AddMinutes, or AddHours... Response.Cookies.Add(cookie); 

Permanent Cookie

 HttpCookie cookie = new HttpCookie("myCookieName", "myCookieValue"); cookie.Expires = DateTime.MaxValue; Response.Cookies.Add(cookie); 
+5
Jun 07 2018-11-11T00:
source share



All Articles