I am working on a MVC website, and in my logout link I want to remove all the current domain cookies.
I tried this:
this.ControllerContext.HttpContext.Response.Cookies.Clear();
and this:
Response.Cookies.Clear();
but both didn't work and the cookies still there.
Add(new HttpCookie("ASP. NET_SessionId", "")); This code example clears the session state from the server and sets the session state cookie to null. The null value effectively clears the cookie from the browser.
In ASP.Net MVC application, a Cookie is created by sending the Cookie to Browser through Response collection (Response. Cookies) while the Cookie is accessed (read) from the Browser using the Request collection (Request.
Cookies are small files that are created in the web browser's memory (if they're temporary) or on the client's hard drive (if they're permanent). CookiesExample.zip. Cookies are one of the State Management techniques, so that we can store information for later use.
What are cookies in C#? Cookies are small units of information that follow all request processes and web pages as they travel between Web browsers and servers. The above definition implies that every web page opened on a website has an exchange of cookies between the server and the web pages.
How about this?
string[] myCookies = Request.Cookies.AllKeys;
foreach (string cookie in myCookies)
{
Response.Cookies[cookie].Expires = DateTime.Now.AddDays(-1);
}
What about this ?
if (Request.Cookies["cookie"] != null)
{
HttpCookie myCookie = new HttpCookie("cookie");
myCookie.Expires = DateTime.Now.AddDays(-1d);
Response.Cookies.Remove(myCookie);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With