我有一个 logout.aspx,当用户单击注销时调用它,我想删除 cookie,但它不会这样做...
public partial class LogoutUser : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie cookie;
cookie = Request.Cookies.Get("Basket");
if (cookie == null)
{
cookie = new HttpCookie("Basket");
}
foreach (string item in cookie.Values.AllKeys)
{
Response.Cookies[item].Expires = DateTime.Now.AddDays(-1);
}
cookie.Expires = DateTime.Now.AddDays(-1d);
Response.Cookies["Basket"].Expires = DateTime.Now.AddYears(-1);
Session.Abandon();
Response.Redirect("~/Default.aspx");
}
}
Cookie 将值存储在购物篮中,但在注销后我仍然可以访问购物篮,我不知道出了什么问题。
最佳答案
Here is some relevant documentation .
我认为你的错误在于这一行:
if (cookie == null)
您正在检查 null,而不是检查 not null。因此,
HttpCookie cookie;
cookie = Request.Cookies.Get("Basket");
if (cookie != null)
{
cookie = new HttpCookie("Basket");
cookie.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(cookie);
}
Response.Redirect("~/Default.aspx");
应该可以解决问题。
希望对您有所帮助。
关于c# - 无法在 ASP.NET C# 中删除 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10095916/