Saturday, August 1, 2009

.NET clear cookies

I try to clear my login cookies in .net by using Request.Cookies.clear(),
it doesn't work at all. Then I did some search online and found out set exired time to pass will solve this problem. I try and still not working, final I set the cookie value to empty string, works!
Notes: Request.Cookies.Clear() is a function to clear current buffer cookies. So confuse by MS.
here is code:

if (Request.Cookies != null && Request.Cookies.Count > 0)
{
foreach (string cookie in Request.Cookies.AllKeys)
{
Request.Cookies[cookie].Expires = DateTime.Now.AddDays(-1);
Response.Cookies[cookie].Value = "";

}

}
}

No comments: