繁体   English   中英

如何在 ASP.NET 网站上删除 cookies

[英]How to delete cookies on an ASP.NET website

在我的网站中,当用户单击“注销”按钮时,Logout.aspx 页面会加载代码Session.Clear()

在 ASP.NET/C# 中,这是否清除了所有 cookies? 或者是否需要添加任何其他代码来删除我网站的所有 cookies?

尝试这样的事情:

if (Request.Cookies["userId"] != null)
{
    Response.Cookies["userId"].Expires = DateTime.Now.AddDays(-1);   
}

但使用也很有意义

Session.Abandon();

除了在许多情况下。

不可以,Cookies 只能通过为它们中的每一个设置到期日期来清洁。

if (Request.Cookies["UserSettings"] != null)
{
    HttpCookie myCookie = new HttpCookie("UserSettings");
    myCookie.Expires = DateTime.Now.AddDays(-1d);
    Response.Cookies.Add(myCookie);
}

Session.Clear()的时刻:

  • 删除Session集合中的所有键值对。 Session_End事件没有发生。

如果您在注销期间使用此方法,您还应该对Session_End事件使用Session.Abandon方法:

  • 带有 Session ID 的 Cookie(如果您的应用程序将 cookies 用于 session id 存储,默认情况下)被删除

这就是我使用的:

    private void ExpireAllCookies()
    {
        if (HttpContext.Current != null)
        {
            int cookieCount = HttpContext.Current.Request.Cookies.Count;
            for (var i = 0; i < cookieCount; i++)
            {
                var cookie = HttpContext.Current.Request.Cookies[i];
                if (cookie != null)
                {
                    var expiredCookie = new HttpCookie(cookie.Name) {
                        Expires = DateTime.Now.AddDays(-1),
                        Domain = cookie.Domain
                    };
                    HttpContext.Current.Response.Cookies.Add(expiredCookie); // overwrite it
                }
            }

            // clear cookies server side
            HttpContext.Current.Request.Cookies.Clear();
        }
    }

不幸的是,对我来说,设置“过期”并不总是有效。 cookie 不受影响。

这段代码对我有用:

HttpContext.Current.Session.Abandon();
HttpContext.Current.Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", ""));

其中"ASP.NET_SessionId"是 cookie 的名称。 这并没有真正删除 cookie,而是用一个空白 cookie 覆盖它,这对我来说已经足够接近了。

我只想指出,正如其他人所说,使用 Session.Abandon 时不会删除 Session ID cookie。

当您放弃 session 时,session ID cookie 不会从用户的浏览器中删除。 Therefore, as soon as the session has been abandoned, any new requests to the same application will use the same session ID but will have a new session state instance. 同时,如果用户在同一个 DNS 域中打开另一个应用程序,则在从一个应用程序调用 Abandon 方法后,用户不会丢失其 session state。

有时,您可能不想重复使用 session ID。 如果您这样做并且如果您了解不重用 session ID 的后果,请使用以下代码示例放弃 session 并清除 Z21D6F40CFB511982E4424E0E250A5 cookie:

 Session.Abandon(); Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", ""));

This code example clears the session state from the server and sets the session state cookie to null. null 值有效地从浏览器中清除 cookie。

http://support.microsoft.com/kb/899918

现在是 2018 年,所以在 ASP.NET 内核中,function 内置了一个直截了当的内容。 要删除 cookie,请尝试以下代码:

if(Request.Cookies["aa"] != null)
{
    Response.Cookies.Delete("aa");
}
return View();

您永远不应将密码存储为 cookie。 要删除 cookie,您实际上只需要修改并使其过期。 您不能真正删除它,即将它从用户的磁盘中删除。

这是一个示例:

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); // make it expire yesterday
        Response.Cookies.Add(aCookie); // overwrite it
    }

以 OP 的问题标题为删除所有 cookies -“在网站中删除 Cookies”

我在某处的 web 上遇到了 Dave Domagala 的代码。 我编辑了 Dave 以允许 Google Analytics cookies 也允许 - 它遍历网站上找到的所有 cookies 并将它们全部删除。 (从开发人员的角度 - 将新代码更新到现有站点中,是避免用户重新访问该站点时出现问题的一个很好的方法)。

我使用下面的代码与首先读取 cookies 一起使用,保存任何所需的数据 - 然后在用下面的循环清洗所有东西后重置 cookies。

编码:

int limit = Request.Cookies.Count; //Get the number of cookies and 
                                   //use that as the limit.
HttpCookie aCookie;   //Instantiate a cookie placeholder
string cookieName;   

//Loop through the cookies
for(int i = 0; i < limit; i++)
{
 cookieName = Request.Cookies[i].Name;    //get the name of the current cookie
 aCookie = new HttpCookie(cookieName);    //create a new cookie with the same
                                          // name as the one you're deleting
 aCookie.Value = "";    //set a blank value to the cookie 
 aCookie.Expires = DateTime.Now.AddDays(-1);    //Setting the expiration date
                                                //in the past deletes the cookie

 Response.Cookies.Add(aCookie);    //Set the cookie to delete it.
}

补充:如果你使用谷歌分析

上述循环/删除将删除该站点的所有 cookies,因此,如果您使用 Google Analytics - 保留 __utmz cookie 可能会很有用,因为该 cookie 会跟踪访问者来自何处、使用了什么搜索引擎、什么点击了链接,使用了什么关键字,以及访问您的网站时它们在世界上的哪个位置。

因此,为了保留它,一旦知道 cookie 名称,就包装一个简单的 if 语句:

... 
aCookie = new HttpCookie(cookieName);    
if (aCookie.Name != "__utmz")
{
    aCookie.Value = "";    //set a blank value to the cookie 
    aCookie.Expires = DateTime.Now.AddDays(-1);   

    HttpContext.Current.Response.Cookies.Add(aCookie);    
}

虽然这是一个旧线程,但我想如果有人仍在寻找未来的解决方案。

HttpCookie mycookie = new HttpCookie("aa");
mycookie.Expires = DateTime.Now.AddDays(-1d);
Response.Cookies.Add(mycookie1);

这就是我的诀窍。

Response.Cookies["UserSettings"].Expires = DateTime.Now.AddDays(-1)

您必须设置过期日期才能删除 cookies

Request.Cookies[yourCookie]?.Expires.Equals(DateTime.Now.AddYears(-1));

如果 cookie 不存在,这不会引发异常。

如果有人喜欢,我强烈推荐这个。

string COOKIE_NAME = "COOCKIE NAME"

if (HttpContext.Request.Cookies.AllKeys.Contains(COOKIE_NAME))
        {
            HttpCookie Cookie = HttpContext.Request.Cookies[COOKIE_NAME];
            engagementIdCookie.Expires = DateTime.Now.AddDays(-1);
            Response.Cookies.Add(Cookie);
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM