我使用 javascript 函数来存储 cookie:
createCookie("teaser", "teaser", 7);
function createCookie(name, value, days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
}
document.cookie = name + "=" + value + expires + "; path=/";
return value;
}
当我检查浏览器中的 cookie 时,teaser 的 cookie 过期设置正确: 25. 2011 年 10 月 16:12:17
但是当我在 C# 中获取值时,到期日期设置为 01.01.0001。
var cookie = Request.Cookies["teaser"];
if (cookie != null && teaserList.Count() > 0)
{
cookie.Expires is 01.01.0001
有什么线索吗?
最佳答案
The browser is responsible for managing cookies, and the cookie's expiration time and date help the browser manage its store of cookies. Therefore, although you can read the name and value of a cookie, you cannot read the cookie's expiration date and time. When the browser sends cookie information to the server, the browser does not include the expiration information. (The cookie's Expires property always returns a date-time value of zero.) If you are concerned about the expiration date of a cookie, you must reset it.
关于c# Asp.net 获取在 javascript 中设置的 Cookie 过期时间总是返回 01.01.0001,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7809072/