I am looking for the best way to clean/clear all existing cookies when they visit the website and are not authenticated.
We don't allow client the ability to 'remember me' to stay logged in. So when they do visit again, what is the best way to start fresh with cookies?
Is it to set all cookies to an empty string? Is it to set the date of the cookies to yesterday?
Any example would be much appreciated.
Daniel K is correct, expiring the cookie is the best option, Your question says you want to clear ALL cookies, you can do this via the Response object:
For Each cookie in Response.Cookies
Response.Cookies(cookie).Expires = DateAdd("d",-1,now())
Next
The problem with setting the cookie to "" - strictly speaking the cookie would still exist, if you want to expire them all so the browser discards them, use .expires
asp.net c# Works fine for me
string[] ck = Request.Cookies.AllKeys;
foreach(string cookie in ck){
Response.Cookies[cookie].Expires = DateTime.Now.AddDays(-1);
}
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