代码之家  ›  专栏  ›  技术社区  ›  Dan Atkinson

设置Expires的HttpCookie返回DateTime。最小值

  •  5
  • Dan Atkinson  · 技术社区  · 16 年前

    在放置饼干时,我看到了一些奇怪的东西。..

    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)
      }
    );
    

    <%= Request.Cookies.Get("foo").Value %>
    

    bar .

    <%= Request.Cookies.Get("foo").Expires %>
    

    01/01/0001 00:00:00

    3 回复  |  直到 16 年前
        1
  •  13
  •   Jon Skeet    16 年前

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

        2
  •  3
  •   yfeldblum    16 年前

    Response.Cookies 这与 Request.Cookies .

        3
  •  1
  •   Chuck Conway    16 年前

    第一

    第二 ,如果您正在设置cookie,然后使用响应。重定向时,您的cookie值可能未被推送到客户端。在封面下回应。重定向调用“Thread.Abort()”,这会杀死线程。