In my website when the user clicks on the "Logout" button, the Logout.aspx page loads with code Session.Clear()
.
In ASP.NET/C#, does this clear all cookies? Or is there any other code that needs to be added to remove all of the cookies of my website?
Though this is an old thread, i thought if someone is still searching for solution in the future.
Thats what did the trick for me.
No, Cookies can be cleaned only by setting the Expiry date for each of them.
At the moment of
Session.Clear()
:Session
collection are removed.Session_End
event is not happen.If you use this method during logout, you should also use the
Session.Abandon
method toSession_End
event:Unfortunately, for me, setting "Expires" did not always work. The cookie was unaffected.
This code did work for me:
where
"ASP.NET_SessionId"
is the name of the cookie. This does not really delete the cookie, but overrides it with a blank cookie, which was close enough for me.It is 2018 now, so in ASP.NET Core, there is a straight forward built in function. To delete a cookie try this code:
I just want to point out that the Session ID cookie is not removed when using Session.Abandon as others said.
http://support.microsoft.com/kb/899918
Taking the OP's Question title as deleting all cookies - "Delete Cookies in website"
I came across code from Dave Domagala on the web somewhere. I edited Dave's to allow for Google Analytics cookies too - which looped through all cookies found on the website and deleted them all. (From a developer angle - updating new code into an existing site, is a nice touch to avoid problems with users revisiting the site).
I use the below code in tandem with reading the cookies first, holding any required data - then resetting the cookies after washing everything clean with the below loop.
The code:
Addition: If You Use Google Analytics
The above loop/delete will delete ALL cookies for the site, so if you use Google Analytics - it would probably be useful to hold onto the __utmz cookie as this one keeps track of where the visitor came from, what search engine was used, what link was clicked on, what keyword was used, and where they were in the world when your website was accessed.
So to keep it, wrap a simple if statement once the cookie name is known: