我正在编写/更新cookie,但是每次我执行此操作并查看chrome dev工具时,它会告诉我Cookie会在30分钟前到期,而不是30分钟后到期。
HttpCookie cookie;
if (Request.Cookies.AllKeys.Contains(name))
{
cookie = Request.Cookies[name];
}
else
{
cookie = new HttpCookie(name);
}
cookie.Value = value;
cookie.Expires = DateTime.Now.AddMinutes(30);
Response.Cookies.SetCookie(cookie);
有谁知道为什么会这样?
答案 0 :(得分:0)
尝试:
var response = HttpContext.Current.Response;
if (Request.Cookies.AllKeys.Contains(name))
{
response.Cookies.Remove(name);
}
HttpCookie cookie = new HttpCookie(name);
cookie.Value = value;
cookie.Expires = DateTime.Now.AddMinutes(30);
response.Cookies.Add(cookie);
或强>
if (Request.Cookies.AllKeys.Contains(name) && Request.Cookies[name]!=null)
{
var cookie = Request.Cookies[name];
cookie.Value = value;
cookie.Expires = DateTime.Now.AddMinutes(30);
Response.Cookies.Set(cookie);//To update a cookie, you need only to set the cookie again using the new values and also you must include all of the data you want to retain.
}
else
{
var cookie = new HttpCookie(name);
cookie.Value = value;
cookie.Expires = DateTime.Now.AddMinutes(30);
Response.Cookies.Add(cookie);
}
答案 1 :(得分:0)
您处于哪个时区?例如,如果您使用的是UTC-1,那么:
这有点像是合理的,所以要带一点点盐!