我正在尝试删除一些由 javascript 设置的 cookie,它在 Firefox 和 Chrome 中运行良好但在 IE 中运行良好,它会删除 IE 中的 cookie 值而不是文件,所以当我加载页面时使用 cookie 它会在删除后加载一些垃圾而不是什么都没有。
我是这样设置cookie的
var exdate = new Date();
exdate.setDate(exdate.getDate() + 1);
var c_value = escape(data.d) + "; expires=" + exdate.toUTCString();
document.cookie = "user" + "=" + data.d;
然后像这样删除
document.cookie = 'user=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
我的问题在哪里?
我在 C# 中也有用于 ASP.Net 删除 cookie 的代码,但它在所有网络浏览器中都不起作用,(它适用于由 C# 而不是使用 Javascript 设置的 cookie)问题出在哪里也有这个代码?
FormsAuthentication.SignOut();
Response.Cookies["user"].Expires = DateTime.Now.AddDays(-1);
Session.Clear();
Response.Cookies.Clear();
最佳答案
您必须将 cookie 发送到响应流,否则您的修改将永远不会提交给浏览器。
我通常使用此代码注销:
FormsAuthentication.SignOut();
CurrentContext.Session.Abandon();
HttpCookie c = CurrentContext.Request.Cookies[FormsAuthentication.FormsCookieName];
if (c != null)
{
c.Expires = DateTime.Now.AddDays(-1);
CurrentContext.Response.Cookies.Add(c);
}
关于c# - 删除 IE 中的 cookie 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8145375/