我有一个页面,用户通过Web服务登录到后端应用程序。 Web服务返回一个会话ID,我想要在cookie中存储40分钟,因为后端应用程序在40分钟后自动关闭会话。C#Cookie - 过期属性不会设置
我的代码写的cookie:
private void SetCookie()
{
Response.Cookies.Add(new HttpCookie("Cookie_SessionID"));
Response.Cookies["Cookie_SessionID"].Value = ni.NicheSessionID;
Response.Cookies["Cookie_SessionID"].Expires = DateTime.Now.AddMinutes(40);
//.... after a few more things
Response.Redirect(returnUrl);
}
然后接收页上我有这样的:
private HttpCookie GetCookie()
{
HttpCookie cookie = Request.Cookies["Cookie_SessionID"];
if (cookie != null && cookie.Value != null)
{
return cookie;
}
return null;
}
出于某种原因,由GetCookie()
返回的cookie的总有0001-01-01 00:00:00
的Expires
值,即使当我在浏览器中查看Cookie时,它也具有正确的到期时间。
看了this其中规定过期的饼干根本就没有发送到服务器,我认为有什么可以发生的是,该cookie正在正确写入但浏览器不发送到期日,因为它实际上是不必要的?...
我的问题是,我想精确捕获 - 该cookie已'过期',因此他们必须再次登录 - 但我需要显示一条消息,沿着“我知道你有已经登录,但你需要再次做“类型的事情。
谢谢