c# - 设置过期的 HttpCookie 返回 DateTime.MinValue

标签 c# asp.net-mvc cookies

我在设置 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/

相关文章:

c# - 强制 WCF 代理生成别名前缀

.net - 如何向 .NET 应用程序添加安全的 FTP 功能

javascript - 为什么 jQuery Selectable 插件不能与 foreach 生成的列表一起使用?

javascript - Cookie 为空,但在浏览器中有一个值

c# - 接口(interface)有多个泛型参数时,如何解决 "cannot implicitly convert [type] to [interface]"错误?

c# - 从命令行调用 Roslyn 分析器

c# - 无法从 Visual Studio Enterprise 2015 连接到 Azure SQL Server

asp.net-mvc - 在 Razor View 中默认区域

javascript - 如何中断 JavaScript 重定向到移动设备?

php - 如何在 Laravel 5 中禁用 cookie?