从浏览器中删除cookie [英] Delete cookie from browsers

查看:88
本文介绍了从浏览器中删除cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在注销时从浏览器中删除cookie,但在使用旧会话ID .AspNet.ApplicationCookie进行测试时。它仍然存在。

我添加到注销的代码是



I am trying to delete cookies from the browser at log out, but when testing with the old session id .AspNet.ApplicationCookie. It still remains.
The code i have added to the log out is

if (System.Web.HttpContext.Current != null)
            {
                HttpCookie aCookie;
                string cookieName;
                int limit = Request.Cookies.Count;
                for (int i = 0; i < limit; i++)
                {
                    cookieName = Request.Cookies[i].Name;
                    aCookie = new HttpCookie(cookieName);
                    aCookie.Expires = DateTime.Now.AddDays(-1d);
                    Response.Cookies.Add(aCookie);
                }
            }
            System.Web.HttpContext.Current.Session.Abandon();
            Response.Cookies.Clear();
            System.Web.HttpContext.Current.Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", ""));





我的尝试:



我尝试将名称设置为空



What I have tried:

I have tried setting the name to null

<pre>cookieName = Request.Cookies[i].Name = null;





并将值设置为null



and setting the value to null

<pre>aCookie.Value = null;

推荐答案

您使用他们的设置指定新cookie,然后调用Response.Cookies.Clear(),以便撤消所有修改。



ASP.net将重新使用会话ID,如果这是一个问题,因为你在代码中的某个地方使用会话ID,那么solu是将你的代码更改为不使用会话ID,而是使用其他东西。
You specify the new cookies with their settings and then you call Response.Cookies.Clear() so all of your amends are undone.

ASP.net will re-use the session id, if that's a problem because you are using the session id somewhere in your code then the solution is to change your code to not use the session id but to use something else.


如果我调用
Response.Cookies.Clear()

在开始或结束时。它没有效果。

at the start or the end. It has no effect.

if (System.Web.HttpContext.Current != null)
{
    System.Web.HttpContext.Current.Session.Clear();
    System.Web.HttpContext.Current.Session.Abandon();
    System.Web.HttpContext.Current.Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", ""));
    HttpCookie aCookie;
    string cookieName;
    int limit = Request.Cookies.Count;
    for (int i = 0; i < limit; i++)
    {
        cookieName = Request.Cookies[i].Name;
        aCookie = new HttpCookie(cookieName);
        aCookie.Expires = DateTime.Now.AddDays(-1);
        Response.Cookies.Add(aCookie);
    }
}





根据会话ID我猜你在谈论



And by the session ID i guess you are talking about

ASP.NET_SessionId







I am trying to delete the <pre>.AspNet.ApplicationCookie


我已经尝试过了。同样的事情发生了。
I have already tried leaving that out too. The same thing happens. The
AspNet.ApplicationCookie

会话在将到期时间设置为提前1天并且根本不调用Clear时仍然有效。

session is still valid after setting the expiry to 1 day earlier and not calling the Clear at all.


这篇关于从浏览器中删除cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆