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
Razor Syntax Error Serializing ASP.NET Model to JSON with HTML.Raw
How to Explain C++ Pointers to a C#/Java Developer
How to Avoid Page Refresh After Button Click Event in Asp.Net
Is Tls 1.1 and Tls 1.2 Enabled by Default for .Net 4.5 and .Net 4.5.1
How to Run Sonarqube Code Analysis for .Net Core (C#) on Linux
How to Get All Input Elements in a Form with HTMLagilitypack Without Getting a Null Reference Error
Mvc-Web API: 405 Method Not Allowed
How to Check If a Given Value Is a Generic List
Return Contents of a Std::Wstring from C++ into C#
Webutility.HTMLdecode Vs Httputilty.HTMLdecode
Linq to Entities Generated SQL
Bind 5 Items in Each Row of Repeater
How to Get Error Information When Httpwebrequest.Getresponse() Fails
SQL Injections with Replace Single-Quotation and Validate Integers
Non-Virtual Interface Design Pattern in C#/C++
File Exists by File Name Pattern
Block Request for Multiple Unsuccessful Logins for a Period of Time