How to Delete Cookies from Windows.Form

How to delete Cookies from windows.form?

If you have JavaScript enabled you can just use this code snippet to clear to clear the cookies for the site the webbrowser is currently on.

webBrowser.Navigate("javascript:void((function(){var a,b,c,e,f;f=0;a=document.cookie.split('; ');for(e=0;e<a.length&&a[e];e++){f++;for(b='.'+location.host;b;b=b.replace(/^(?:%5C.|[^%5C.]+)/,'')){for(c=location.pathname;c;c=c.replace(/.$/,'')){document.cookie=(a[e]+'; domain='+b+'; path='+c+'; expires='+new Date((new Date()).getTime()-1e11).toGMTString());}}}})())")

It's derived from this bookmarklet for clearing cookies.

How to Delete the cookies from Windows form Browser C#?

The Browser does manage the cookies.
In theory deleting the cookies is possible by locating the file, which holds the cookies and deleting the file or its content.
But the location and type (could be a simple textfile or a database) of this file is different for each browser and can differ by version too. So there is no generic way to do this.

A trick to remove individual cookies indirectly is to set the expiring date to the past and refreshing the page:

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

How to Delete the cookies from Windows form Browser C#?

The Browser does manage the cookies.
In theory deleting the cookies is possible by locating the file, which holds the cookies and deleting the file or its content.
But the location and type (could be a simple textfile or a database) of this file is different for each browser and can differ by version too. So there is no generic way to do this.

A trick to remove individual cookies indirectly is to set the expiring date to the past and refreshing the page:

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

How to delete cookies on an ASP.NET website

Try something like that:

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

But it also makes sense to use

Session.Abandon();

besides in many scenarios.

Delete cookies in .net core 2.0

In ASP.NET Core, you can/should use the following method:

    private void DeleteCookies()
{
foreach (var cookie in HttpContext.Request.Cookies)
{
Response.Cookies.Delete(cookie.Key);
}
}

What this does internally is to send 'Set-Cookie' directives in the Http Response Header to instruct the browser to both expire the cookie and clear its value.

  • ASP.NET Core source code for ResponseCookies.Delete Method
  • MSDN docs here - IResponseCookies.Delete Method

Example response header:

HTTP/1.1 302 Found
Cache-Control: no-cache
Pragma: no-cache
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Server: Microsoft-IIS/10.0
Set-Cookie: Cookie1=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; secure; samesite=lax
Set-Cookie: Cookie2=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; secure; samesite=lax
Set-Cookie: Cookie3=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; secure; samesite=lax


Related Topics



Leave a reply



Submit