Cookie 过期时间在 C# 中不起作用
Cookie expiration time not working in C#
我无法设置持久性 cookie。它只成为会话。
我知道 here 也遇到过同样的问题,但即使我完成了所有步骤也无法解决。
(无法使用 Fiddler 进行测试)。
我的代码是:
/// Default.aspx.cs methods :
protected void Page_Load(object sender, EventArgs e)
{
if (! Cookies.CookieExist("kuki"))
{
Cookies.CreateCookie("kuki");
}
Fill();
}
private void Fill()
{
string a = Cookies.GetCookieValue("kuki", "key");
if (!a.Contains("data"))
{
return;
}
// codes for a
}
protected void ButtonAdd_Click(object sender, EventArgs e)
{
if (TextBox1.Text != string.Empty)
{
Cookies.InsertCookie("kuki", "key", TextBox1.Text);
}
}
/// Cookie class methods:
public static void InsertCookie(string Cookie, string Key, string Data)
{
HttpCookie Kuki = HttpContext.Current.Request.Cookies[Cookie];
Kuki[Key] = Data;
HttpContext.Current.Response.SetCookie(Kuki);
}
public static bool CookieExist(string Cookie)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[Cookie];
if (cookie == null)
{
return false;
}
else
{
return true;
}
}
public static void CreateCookie(string Cookie)
{
HttpCookie cookie = new HttpCookie(Cookie);
cookie.Expires = DateTime.Now.AddYears(1);
HttpContext.Current.Response.Cookies.Set(cookie);
}
public static string GetCookieValue(string CookieName, string CookieKey)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[CookieName];
try
{
if (cookie != null)
{
return cookie[CookieKey].ToString();
}
else
{
return "";
}
}
catch (Exception)
{
return "";
}
}
我用Chrome.
我该如何解决这个问题?
更新:
添加了按钮和 InsertCookie 方法。
而不是
public static void CreateCookie(string Cookie)
{
HttpCookie cookie = new HttpCookie(Cookie);
cookie.Expires = DateTime.Now.AddYears(1);
HttpContext.Current.Response.Cookies.Set(cookie);
}
尝试
public static void CreateCookie(string Cookie)
{
HttpCookie cookie = new HttpCookie(Cookie);
cookie.Expires = DateTime.Now.AddYears(1);
HttpContext.Current.Response.Cookies.Add(cookie); //<-- Add
}
当您需要更新已写入响应的 cookie(几乎永远不会)时,请使用 Add
to add a cookie. Only use Set
。
我明白错误是什么了。
我试图将数据存储在我的 cookie 上。
但是我应该只将我的 cookie 用于记住我的方法,例如。
对不起。
我无法设置持久性 cookie。它只成为会话。
我知道 here 也遇到过同样的问题,但即使我完成了所有步骤也无法解决。
(无法使用 Fiddler 进行测试)。
我的代码是:
/// Default.aspx.cs methods :
protected void Page_Load(object sender, EventArgs e)
{
if (! Cookies.CookieExist("kuki"))
{
Cookies.CreateCookie("kuki");
}
Fill();
}
private void Fill()
{
string a = Cookies.GetCookieValue("kuki", "key");
if (!a.Contains("data"))
{
return;
}
// codes for a
}
protected void ButtonAdd_Click(object sender, EventArgs e)
{
if (TextBox1.Text != string.Empty)
{
Cookies.InsertCookie("kuki", "key", TextBox1.Text);
}
}
/// Cookie class methods:
public static void InsertCookie(string Cookie, string Key, string Data)
{
HttpCookie Kuki = HttpContext.Current.Request.Cookies[Cookie];
Kuki[Key] = Data;
HttpContext.Current.Response.SetCookie(Kuki);
}
public static bool CookieExist(string Cookie)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[Cookie];
if (cookie == null)
{
return false;
}
else
{
return true;
}
}
public static void CreateCookie(string Cookie)
{
HttpCookie cookie = new HttpCookie(Cookie);
cookie.Expires = DateTime.Now.AddYears(1);
HttpContext.Current.Response.Cookies.Set(cookie);
}
public static string GetCookieValue(string CookieName, string CookieKey)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[CookieName];
try
{
if (cookie != null)
{
return cookie[CookieKey].ToString();
}
else
{
return "";
}
}
catch (Exception)
{
return "";
}
}
我用Chrome.
我该如何解决这个问题?
更新:
添加了按钮和 InsertCookie 方法。
而不是
public static void CreateCookie(string Cookie)
{
HttpCookie cookie = new HttpCookie(Cookie);
cookie.Expires = DateTime.Now.AddYears(1);
HttpContext.Current.Response.Cookies.Set(cookie);
}
尝试
public static void CreateCookie(string Cookie)
{
HttpCookie cookie = new HttpCookie(Cookie);
cookie.Expires = DateTime.Now.AddYears(1);
HttpContext.Current.Response.Cookies.Add(cookie); //<-- Add
}
当您需要更新已写入响应的 cookie(几乎永远不会)时,请使用 Add
to add a cookie. Only use Set
。
我明白错误是什么了。 我试图将数据存储在我的 cookie 上。 但是我应该只将我的 cookie 用于记住我的方法,例如。 对不起。