由客户端取得的cookie之中Expires属性为默认值0001-1-1 0:00:00!
因此不要指望
HttpCookie cookie = Request.Cookies["A"];
if (cookie == null)
{
cookie = new HttpCookie("A", "0");
cookie.Expires = DateTime.Now.AddDays(1);
cookie.Value = 45.ToString();
Response.AppendCookie(cookie);
}
else
{
cookie.Value = (1 + int.Parse(cookie.Value)).ToString();
}
if (cookie == null)
{
cookie = new HttpCookie("A", "0");
cookie.Expires = DateTime.Now.AddDays(1);
cookie.Value = 45.ToString();
Response.AppendCookie(cookie);
}
else
{
cookie.Value = (1 + int.Parse(cookie.Value)).ToString();
}
这样的代码会把修改后的cookie放回到客户端, 由于过期时间被重置为DefualtValue, 这个cookie将变为一个Session cookie。如果需要保存Cookie, 必须在每一次获取Cookie后设置过期。
搞的有点晕