1

I have need to delete the cookies from the browser . I can do it manually by going to the cookies and by deleting it . How can i do it programmatically ?

deleting cookies from browser

Souad
  • 4,856
  • 15
  • 80
  • 140
Sam Ganguly
  • 77
  • 1
  • 5
  • 1
    Have you tried [something like this](http://stackoverflow.com/questions/10593013/delete-cookie-by-name) – George Feb 15 '17 at 15:15
  • Does this answer your question? http://stackoverflow.com/questions/2144386/javascript-delete-cookie – webdevduck Feb 15 '17 at 15:15
  • 1
    What have you tried? Are you wanting to delete all cookies or just cookies previously created by your site? – adam Feb 15 '17 at 15:16
  • If you are doing lots of cookie manipulation then something like [js-cookie](https://github.com/js-cookie/js-cookie) will make your life a lot easier – John M Feb 15 '17 at 15:17
  • I am trying to delete all the cookies by https://login.microsoftonline.com/ ...But even deleting all the cookies will do my job for now . – Sam Ganguly Feb 15 '17 at 15:18
  • @Adam I have tried if (Request.Cookies["UserSettings"] != null) { HttpCookie myCookie = new HttpCookie("UserSettings"); myCookie.Expires = DateTime.Now.AddDays(-1d); Response.Cookies.Add(myCookie); } and string[] cookies = Request.Cookies.AllKeys; foreach (string cookie in cookies) { Response.Cookies[cookie].Expires = DateTime.Now.AddDays(-1); Response.Cookies[cookie].Value = ""; } – Sam Ganguly Feb 15 '17 at 15:18

2 Answers2

1

You can do it in your .Net Application by first removing it and then adding it with passed expiration. Try using the following code:

if (Request.Cookies["UserSettings"] != null)
{
   HttpContext.Current.Response.Cookies.Remove("UserSettings");
   HttpCookie myCookie = new HttpCookie("UserSettings");
   myCookie.Expires = DateTime.Now.AddDays(-1d);
   myCookie.Value = null;
   HttpContext.Current.Response.SetCookie(myCookie);
}
Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
S.Dav
  • 2,436
  • 16
  • 22
0

For security reasons, you can't access to third party cookies. I mean that you can't modify or delete cookies created by other domains different than yours.

I hope this helps

Carles
  • 418
  • 2
  • 18
  • Can i write a code that will delete these cookies from the browser ? – Sam Ganguly Feb 15 '17 at 15:28
  • Not from Javascript, but externally, cookies are usually saved to a folder, depending on the browser and operating system. You in some cases you could write an external program or script to perform that task if you have enough permissions. – Carles Feb 15 '17 at 15:30