Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why not using Request.Cookies.Clear() in ASP.NET web forms?

I searched here on the stackoverflow about removing all cookies from site, but couldn't find a single answer suggesting the use of Request.Cookies.Clear() method.

What's the difference between:

if (Request.Cookies["UserSettings"] != null)
{
    HttpCookie myCookie = new HttpCookie("UserSettings");
    myCookie.Expires = DateTime.Now.AddDays(-1d);
    Response.Cookies.Add(myCookie);
}

and:

Request.Cookies.Clear();

Thanks in advance! and sorry for my bad language, English is not my native!

like image 853
Tm9hbQn Avatar asked May 18 '13 13:05

Tm9hbQn


1 Answers

Calling Remove or Clear will remove it from the server side collection held by Request.Cookies (which is a copy of the cookies your client sent to you). However that does not cause the server to instruct the client browser to remove the cookie. To do that you need to set the timeout as you have indicated above (see MSDN - How To: Delete a Cookie for the official guidance).

like image 168
Andy Brown Avatar answered Oct 13 '22 05:10

Andy Brown