c#Asp.net在JavaScript中设置Cookie过期时间始终返回01.01.0001 [英] c# Asp.net getting Cookie expiration time set in javascript always returns 01.01.0001
问题描述
我使用javascript函数存储cookie:
I use a javascript function to store the 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时,预告片的Cookie过期是正确的设置:
25. oktober 2011 16:12:17
And when I check the cookies in my browser the cookie expiration of teaser is correctly set: 25. oktober 2011 16:12:17
但是当我在C#中获取该值时,到期日期设置为01.01.0001 。
But when in C# i go to get the value, the expiration date is set to 01.01.0001.
var cookie = Request.Cookies["teaser"];
if (cookie != null && teaserList.Count() > 0)
{
cookie.Expires is 01.01.0001
任何线索?
推荐答案
Egghead says that:
浏览器负责管理Cookie,cookie的
到期时间和日期有助于浏览器管理其商店的饼干。
因此,虽然您可以读取cookie的名称和值,但
无法读取cookie的到期日期和时间。当浏览器
向服务器发送cookie信息时,浏览器不包含
过期信息。 (cookie的Expires属性始终为
返回一个日期时间值为零。)如果您担心cookie的
到期日期,您必须重新设置。
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!