How to read the cookie creation date (not expiration)

女生的网名这么多〃 提交于 2019-12-01 01:38:48

问题


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.


回答1:


<!-- 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()



回答2:


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.



来源:https://stackoverflow.com/questions/17722084/how-to-read-the-cookie-creation-date-not-expiration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!