Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I clean out all cookies?

Tags:

asp-classic

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.

like image 859
James Wilson Avatar asked Dec 10 '22 00:12

James Wilson


2 Answers

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

like image 144
HeavenCore Avatar answered Feb 01 '23 15:02

HeavenCore


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);
}
like image 38
Faisal Naseer Avatar answered Feb 01 '23 14:02

Faisal Naseer