为什么cookie的有效期限不跨会话生存在ASP.NET? [英] Why is the cookie expiration date not surviving across sessions in ASP.NET?

查看:97
本文介绍了为什么cookie的有效期限不跨会话生存在ASP.NET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一些更改测试平台的页面,所以我可以让我的问题在这里清晰。

I made some changes to the testbed page, so I could make my question clearer here.

该页面有三个按钮:设置;明确;而得到的。

The page has three buttons: Set; Clear; and Get.

设置有这个code:

PreferredCookie = new HttpCookie("PreferredCookie");
PreferredCookie.Value = "Chocolate Chip";
DateTime exp = DateTime.Now.AddDays(1.0d);
PreferredCookie.Expires = exp;
Response.Cookies.Set(PreferredCookie);

清除有这样的:

HttpCookie PreferredCookie = Request.Cookies["PreferredCookie"];
if (PreferredCookie != null)
{
    PreferredCookie.Value = "";
    PreferredCookie.Expires = DateTime.Now;
    Response.Cookies.Set(PreferredCookie);
}

获取有这个,它输出到一个asp:文字:

Get has this, which outputs to an asp:Literal:

HttpCookie PreferredCookie = Request.Cookies["PreferredCookie"];
if (PreferredCookie != null)
{
    CookieLiteral.Text = "Value = " + PreferredCookie.Value + "<br>";
    CookieLiteral.Text += "Expires = " + PreferredCookie.Expires.ToString("MM/dd/yyyy HH:mm:ss");
}
else
{
    CookieLiteral.Text = "<h2>No Cookie?</h2>";
}

如果我开始页面,点击清除,然后跟进的东西,我看到:

If I start the page and click on Clear, and then follow-up with the Get, I see:

没有饼干吗?

如果我然后单击设置,然后拿到,我看到:

If I then click the Set, then Get, I see:

值=巧克力片结果
  过期01/01/0001 = 00:00:00

Value = Chocolate Chip
Expires = 01/01/0001 00:00:00

这个日期似乎被视为永不过期。我得到同样的结果,如果我访问与Firefox的页面。

This date seems to be treated as never expiring. I get the same results if I access the page with Firefox.

推荐答案

简短的答案 - 您无法读取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的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的属性
  型Htt presponse对象,之前该cookie被发送给浏览器。
  但是,你不能获得期满回到了Htt prequest对象。

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.

所以基本上,cookie过期日期设置正确。这可以通过检查在浏览器cookie的验证。不幸的是,看完这个cookie就像在你的获取函数将返回1/1/0001。

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的有效期限不跨会话生存在ASP.NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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