I am using the following code to set a cookie in my asp.net mvc(C#) application:
public static void SetValue(string key, string value, DateTime expires) { var httpContext = new HttpContextWrapper(HttpContext.Current); _request = httpContext.Request; _response = httpContext.Response; HttpCookie cookie = new HttpCookie(key, value) { Expires = expires }; _response.Cookies.Set(cookie); }
I need to delete the cookies when the user clicks logout. The set cookie is not removing/deleting with Clear/Remove. The code is as below:
public static void Clear() { var httpContext = new HttpContextWrapper(HttpContext.Current); _request = httpContext.Request; _response = httpContext.Response; _request.Cookies.Clear(); _response.Cookies.Clear(); } public static void Remove(string key) { var httpContext = new HttpContextWrapper(HttpContext.Current); _request = httpContext.Request; _response = httpContext.Response; if (_request.Cookies[key] != null) { _request.Cookies.Remove(key); } if (_response.Cookies[key] != null) { _response.Cookies.Remove(key); } }
I have tried both the above functions, but still the cookie exists when i try to check exist.
public static bool Exists(string key) { var httpContext = new HttpContextWrapper(HttpContext.Current); _request = httpContext.Request; _response = httpContext.Response; return _request.Cookies[key] != null; }
What may be problem here? or whats the thing i need to do to remove/delete the cookie?
Your cookie is most likely not being deleted because when you set the new value, it has to match the path and domain of the original cookie you're trying to delete. that "something" value needs to line up with whatever the existing cookies have set.
If bad cookies keep making their way back onto your company computer after a scanner removes them, it is because your Web browsing keeps inviting the cookie back.
On your device, launch the Chrome app. At the top right, tap on the three dots, scroll down, and then select “Settings.” Under settings, tap “Privacy” then “Clear browsing data.” Select “Cookies, site data” and uncheck all other items.
Clearing the cookies of the response doesn't instruct the browser to clear the cookie, it merely does not send the cookie back to the browser. To instruct the browser to clear the cookie you need to tell it the cookie has expired, e.g.
public static void Clear(string key) { var httpContext = new HttpContextWrapper(HttpContext.Current); _response = httpContext.Response; HttpCookie cookie = new HttpCookie(key) { Expires = DateTime.Now.AddDays(-1) // or any other time in the past }; _response.Cookies.Set(cookie); }
The Cookies collection in the Request and Response objects aren't proxies for the cookies in the browser, they're a set of what cookies the browser sends you and you send back. If you remove a cookie from the request it's entirely server side, and if you have no cookies in the response you're just not going to send any thing back to the client, which won't change the set of cookies in the browser at all.
To delete a cookie, make sure that it is in the response cookie collection, but has an expiration time in the past.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With