设置Expires的HttpCookie返回DateTime.MinValue [英] HttpCookie with Expires set returns DateTime.MinValue

查看:127
本文介绍了设置Expires的HttpCookie返回DateTime.MinValue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置cookie时,我发现有些奇怪...

I'm seeing something of an oddity when setting a 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的到期时间为一个小时,并且但是,当我查看视图中的cookie时,到期时间就不存在了……

When debugging, I can see that this new cookie has an expiry of one hour in the future, and yet, when I look at the cookie in the view, the expiry isn't there...

视图:

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

返回 bar

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

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

有什么想法吗?!

推荐答案

查看请求-其中不包含到期时间。服务器告诉客户端cookie何时到期。客户端也无需告诉服务器:)

You're looking at the request - which doesn't contain an expiry time. The server tells the client when the cookie should expire; there's no need for the client to tell the server as well :)

这篇关于设置Expires的HttpCookie返回DateTime.MinValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆