带有Expires set的HttpCookie返回DateTime.MinValue

Dan*_*son 4 c# cookies asp.net-mvc

我在设置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)
  }
);
Run Code Online (Sandbox Code Playgroud)

在调试时,我可以看到这个新cookie将来会有一个小时到期,然而,当我在视图中查看cookie时,到期时间不存在......

视图:

<%= Request.Cookies.Get("foo").Value %>
Run Code Online (Sandbox Code Playgroud)

退货bar.

<%= Request.Cookies.Get("foo").Expires %>
Run Code Online (Sandbox Code Playgroud)

返回 01/01/0001 00:00:00

有任何想法吗?!

Jon*_*eet 13

您正在查看请求 - 其中不包含到期时间.服务器告诉客户端cookie何时到期; 客户端也不需要告诉服务器:)