我在设置 cookie 时看到了一些奇怪的东西......
行动:
string cookieName = "foo";
string cookieValue = "bar";
//Set a cookie in the response, along with the Expires.
this.ControllerContext.HttpContext.Response.Cookies.Add(
new HttpCookie(cookieName, cookieValue)
{
Expires = DateTime.Now.AddHours(1)
}
);
调试时,我可以看到这个新的 cookie 有一个小时后的到期时间,但是,当我在 View 中查看 cookie 时,却没有到期...
查看:
<%= Request.Cookies.Get("foo").Value %>
返回 bar
。
<%= Request.Cookies.Get("foo").Expires %>
返回 01/01/0001 00:00:00
有什么想法吗?!
最佳答案
您正在查看请求 - 它不包含到期时间。服务器告诉客户端 cookie 何时过期;客户端也不需要告诉服务器:)
关于c# - 设置过期的 HttpCookie 返回 DateTime.MinValue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1676901/