How to Delete Cookies on an ASP.NET Website

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.

How to delete or expire cookie in Chrome using asp.net

As mentioned in the comment, This could be because chrome setting "Continue where you left off".

You can cross check in a different browser.

Chrome Doesn't Delete Session Cookies

how to delete cookies in asp.net

From the documentation:

You cannot directly delete a cookie on
a user's computer. However, you can
direct the user's browser to delete
the cookie by setting the cookie's
expiration date to a past date. The
next time a user makes a request to a
page within the domain or path that
set the cookie, the browser will
determine that the cookie has expired
and remove it.

So, your strategy is the right one, and the cookie should disappear from the browser once the response is received.

how to remove cookies from my website

ASP.Net has by default session state (https://msdn.microsoft.com/en-us/library/ms178581.aspx) turned on, which in turn relies on cookies by default. If you don't need session state (because users don't logon etc) you can turn it off in the web.config file. Just add

<sessionState mode="Off"> 

under the

<system.web>

element.

How do I manually delete a cookie in asp.net MVC 4

Try:

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

More information on MSDN.

Clear all cookies in Asp.net Core

Request.Cookies is a key-value collection where the Key is a cookie name. So

foreach (var cookie in Request.Cookies.Keys)
{
Response.Cookies.Delete(cookie);
}

See:

public abstract class HttpRequest
{
// Summary:
// /// Gets the collection of Cookies for this request. ///
//
// Returns:
// The collection of Cookies for this request.
public abstract IRequestCookieCollection Cookies { get; set; }
...
}

and IRequestCookieCollection is

public interface IRequestCookieCollection : IEnumerable<KeyValuePair<string, string>>, IEnumerable

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