我有一个 Asp.Net 网站,有2个cookie。
我通过此代码创建这些 cookie;
public static void CreateCookie(string Cookie)
{
HttpCookie cookie = new HttpCookie(Cookie);
cookie.Expires = DateTime.Now.AddYears(1);
HttpContext.Current.Response.Cookies.Add(cookie);
}
CreateCookie("Cookie1");
CreateCookie("Cookie2");
因此,正如我在Chrome上看到的那样,这两个cookie的生命周期都是1年。
但是当我在 1 天后检查时,我看到 Cookie1 被自动删除,
尽管我在没有进入网站的情况下检查 cookie,只是检查 Chrome。
我无法阻止这一点。为什么会这样?
我该如何解决这个问题?
更新:
此错误仅发生在我自己的网站上。
我将在另一台电脑和另一台浏览器上检查这一点。
您没有为 Cookie 设置值。添加没有值的 cookie 将使浏览器删除具有给定名称的 cookie(如果存在)。
使用值:
public static void CreateCookie(string Cookie, string value)
{
HttpCookie cookie = new HttpCookie(Cookie, value);
cookie.Expires = DateTime.Now.AddYears(1);
HttpContext.Current.Response.Cookies.Add(cookie);
}
CreateCookie("Cookie1", "Hello");
CreateCookie("Cookie2", "world");
尝试通过以下方式将 cookie 链接到会话:
HttpCookie authCookie = new HttpCookie(Cookie, Session.SessionID);