简体   繁体   English

c#Asp.net获取javascript中设置的Cookie到期时间始终返回01.01.0001

[英]c# Asp.net getting Cookie expiration time set in javascript always returns 01.01.0001

I use a javascript function to store the cookie: 我使用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;
}

And when I check the cookies in my browser the cookie expiration of teaser is correctly set: 25. oktober 2011 16:12:17 当我在浏览器中检查cookie时,预告片的cookie到期时间设置正确:25。oktober 2011 16:12:17

But when in C# i go to get the value, the expiration date is set to 01.01.0001. 但是当在C#中我去获取值时,到期日期设置为01.01.0001。

    var cookie = Request.Cookies["teaser"];
    if (cookie != null && teaserList.Count() > 0)
    {

        cookie.Expires is 01.01.0001

Any clue? 任何线索?

Egghead says that: Egghead说:

The browser is responsible for managing cookies, and the cookie's expiration time and date help the browser manage its store of cookies. 浏览器负责管理cookie,cookie的过期时间和日期有助于浏览器管理其cookie存储。 Therefore, although you can read the name and value of a cookie, you cannot read the cookie's expiration date and time. 因此,虽然您可以读取cookie的名称和值,但您无法读取cookie的到期日期和时间。 When the browser sends cookie information to the server, the browser does not include the expiration information. 当浏览器向服务器发送cookie信息时,浏览器不包含过期信息。 (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. (cookie的Expires属性始终返回零日期时间值。)如果您担心cookie的到期日期,则必须重置它。

The browser does not transmit the expiration date of a cookie to servers, this is as per HTTP specification. 浏览器不会将cookie的过期日期传输到服务器,这是根据HTTP规范。

Browsers only send the cookie name and value only . 浏览器只发送cookie名和唯一的价值。

The Expires property on the cookie object is only used when setting expiration date on a cookie that is going to be written in a response, not read in a request. Cookie对象上的Expires属性仅在要在响应中写入的cookie上设置到期日期时使用,而不是在请求中读取

When reading the cookie is Expires property value is always going to be null. 读取cookie时,Expires属性值始终为null。 You can see this for yourself by examining the HTTP headers. 您可以通过检查HTTP标头自行查看。 If you really want to read the expire value of a cookie, try writing it in the value itself, or using another cookie with the value of the expiration date (you will need to keep them in sync yourself). 如果您确实想要读取cookie的过期值,请尝试将其写入值本身,或者使用具有过期日期值的其他cookie(您需要自己保持同步)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM