Introduction:
In my previous article I
have explained about How
to Add httpcookie using C#.net. In this article I am going to explain about how to delete the
browser cookies using C#.Net code.
Explanation:
You
cannot directly delete a cookie on a user's computer. However, you can direct
the user's browser to delete the cookie by setting the cookie's expiration date
to a past date.
To assign a past expiration date on a cookie
1. Determine
whether the cookie exists, and if so, create a new cookie with the same name.
2. Set
the cookie's expiration date to a time in the past.
Below is the method
used to delete the cookie.
protected void DeleteCookie(string cookiename)
{
if
(Request.Cookies[cookiename] != null)
{
HttpCookie
myCookie = new HttpCookie(cookiename);
myCookie.Expires = DateTime.Now.AddDays(-1d);
Response.Cookies.Add(myCookie);
}
}
Sample usage of
the method is given below.
DeleteCookie("name");
Do
you like this article? Help us to improve. If you have any queries post it in
comments. We will give you the solution.
Comments
Post a Comment