Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read the cookie creation date (not expiration)

Is there a way to store in a variable a cookie creation date? I'm using the jquery.cookie plugin. If there is not a way, I'm thinking about store in the cookie, as value, the actual time/date. It could be a solution.

Thanks.

like image 254
Mario Lima Cavalcanti Avatar asked Jul 18 '13 11:07

Mario Lima Cavalcanti


2 Answers

You will indeed have to store the time in the cookie itself. The browser's cookie API does not supply the creation date as metadata.

like image 154
Ulrich Schmidt-Goertz Avatar answered Sep 20 '22 21:09

Ulrich Schmidt-Goertz


<!-- Output the DateTime that the cookie is set to expire -->
@Request.Cookies["YourCookie"].Expires.ToString()

However, I don't believe that there is a property to get the Creation Date, unless you were to specifically store the value itself as an additional value within the Cookie itself :

//Create your cookie
HttpCookie yourCookie = new HttpCookie("Example");
//Add an actual value to the Values collection
yourCookie.Values.Add("YourValue", "ExampleValue");
//Add a Created Value to store the DateTime the Cookie was created
yourCookie.Values.Add("Created", DateTime.Now.ToString());
yourCookie.Expires = DateTime.Now.AddMinutes(30);

//Add the cookie to the collection
Request.Cookies.Add(yourCookie);

which you could access in your page through :

Created : @Request.Cookies["Example"].Values["Created"].ToString()
Expires : @Request.Cookies["Example"].Expires.ToString()
like image 21
Ganesh Rengarajan Avatar answered Sep 21 '22 21:09

Ganesh Rengarajan