c#Asp.net获取Cookie过期时间在javascript中设置总是返回01.01.0001 [英] c# Asp.net getting Cookie expiration time set in javascript always returns 01.01.0001

查看:418
本文介绍了c#Asp.net获取Cookie过期时间在javascript中设置总是返回01.01.0001的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用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 = /;
返回值;
}

当我在浏览器中检查cookie时,teaser的cookie过期正确set:
25. oktober 2011 16:12:17



但是当在C#中去获取值,到期日期设置为01.01.0001 。

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

cookie.Expires是01.01.0001



任何线索?

解决方案

Egghead说: p>


浏览器负责管理Cookie,Cookie的
过期时间和日期有助于浏览器管理其Cookie存储。
因此,虽然您可以读取cookie的名称和值,但您
无法读取cookie的到期日期和时间。当浏览器
发送cookie信息到服务器时,浏览器不包括
的过期信息。 (Cookie的Expires属性总是
返回一个日期 - 时间值零)。如果你关心
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;
}

And when I check the cookies in my browser the cookie expiration of teaser is correctly set: 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.

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

        cookie.Expires is 01.01.0001

Any clue?

解决方案

Egghead says that:

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获取Cookie过期时间在javascript中设置总是返回01.01.0001的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆