Cookie有效期始终为1/1/0001 [英] Cookie expiry date is always 1/1/0001

查看:175
本文介绍了Cookie有效期始终为1/1/0001的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说明:

我在mvc项目中使用cookie来记住用户选择的最新下载格式。创建Cookie时,我要设置该Cookie的到期日期。当我尝试获取该Cookie和该Cookie的失效日期时,我总是得到 1/1/0001作为失效日期。我不知道背后的原因。

I'm using cookie in mvc project to remember most recent download format selected by user. While creating cookie, I'm setting expiry date for that cookie. And when I try to get that cookie and expiry date of that cookie then I'm getting "1/1/0001" as expiry date always. I'm not getting the reason behind this. please help to reason behind this.

代码:

1)设置cookie及其有效期:

 Response.Cookies.Add(new HttpCookie(base.LoggedOnUser.Email, exportFileFormat.ToString()));
                    var requestCookie = Request.Cookies[base.LoggedOnUser.Email];
                    if (requestCookie != null)
                    {
                        requestCookie.Expires = DateTime.UtcNow.AddDays(Convert.ToInt32(ConfigurationManager.AppSettings["FileFormatTypeCookieExpiry"]));
                    }

2)获取cookie及其有效期:

 var fileFormatTypeCookie = HttpContext.Current.Request.Cookies[CurrentUser.Email];
                        if (fileFormatTypeCookie != null && fileFormatTypeCookie.Value != null)
                        {
                            var exportFileFormat = fileFormatTypeCookie.Value;
                            var expiry = fileFormatTypeCookie.Expires;

                        }

以上变量有效期始终为 1/1/0001 。

Above variable expiry is always "1/1/0001".

推荐答案

我引用了MikeSmithDev的答案,内容可能是重复的问题:

I quote the answer from MikeSmithDev from a possible duplicate question:

为什么


简短答案-您无法读取Cookie的过期日期,并且
时间。

The Short Answer - You cannot read the cookie's expiration date and time.

稍长的答案-这不是ASP.NET中的会话问题。
这是在
ASP.NET中可以从cookie服务器端读取的内容的问题。根据MSDN:

Slightly Longer Answer - This is not an issue of sessions in ASP.NET. It is an issue of what you can read from a cookie server-side in ASP.NET. Per the MSDN:

浏览器负责管理cookie,而cookie的
过期时间和日期可帮助浏览器管理其cookie存储。
因此,尽管您可以读取Cookie的名称和值,但是
无法读取Cookie的到期日期和时间。当浏览器
向服务器发送cookie信息时,浏览器不包括
到期信息。 (该Cookie的Expires属性始终
返回的日期时间值为零。)

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.)

您可以读取已在Cookie中设置的Cookie的Expires属性。
HttpResponse对象,然后将cookie发送到浏览器。
但是,您无法在HttpRequest对象中获得到期。
因此,基本上,cookie的有效日期已正确设置。可以通过在浏览器中检查cookie来验证
。不幸的是,
像在Get函数中一样读取该cookie会返回1/1/0001。

You can read the Expires property of a cookie that you have set in the HttpResponse object, before the cookie has been sent to the browser. However, you cannot get the expiration back in the HttpRequest object. So basically, the cookie expiration date is set correctly. This can be verified by inspecting the cookie in the browser. Unfortunately, reading this cookie like in your Get function will return 1/1/0001.

如果您真的想获得到期,那么您会必须将其
存储在cookie本身中:

If you really want to get the expiration, then you'd have to store it in the cookie itself:

设置

DateTime exp = DateTime.Now.AddDays(1); 
HttpCookie PreferredCookie = new HttpCookie("PreferredCookie"); 
PreferredCookie.Values.Add("cookieType", "Zref"); 
PreferredCookie.Values.Add("exp", exp.ToString());
PreferredCookie.Expires = exp; 
Response.Cookies.Set(PreferredCookie); 

获取

HttpCookie PreferredCookie = Request.Cookies["PreferredCookie"];
if (PreferredCookie != null)
{
  CookieLiteral.Text = "Value = " + PreferredCookie["cookieType"] + "<br>";
  CookieLiteral.Text += "Expires = " + PreferredCookie["exp"];
}
else
{
  CookieLiteral.Text = "No Cookie"; 
}

这篇关于Cookie有效期始终为1/1/0001的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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