0

I am working on a webbrowser control on a Windows.form in c#. I have been trying to figure out a way to delete all cookies in my webbrowser. I have tried this without success.

webBrowser.Navigate("javascript:void((function(){var a,b,c,e,f;f=0;a=document.cookie.split('; ');for(e=0;e<a.length&&a[e];e++){f++;for(b='.'+location.host;b;b=b.replace(/^(?:%5C.|[^%5C.]+)/,'')){for(c=location.pathname;c;c=c.replace(/.$/,'')){document.cookie=(a[e]+'; domain='+b+'; path='+c+'; expires='+new Date((new Date()).getTime()-1e11).toGMTString());}}}})())")

I also tried to delete all cookies like so.

string[] Cookies = System.IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Cookies));
                int notDeleted = 0;
                foreach (string CookieFile in Cookies)
                {
                    try
                    {
                        System.IO.File.Delete(CookieFile);

                    }
                    catch (Exception ex)
                    {
                        notDeleted++;
                    }

                }

This second way worked for me but only after I close the windows form and then open it. It seems to only delete all cookies after the windows form is closed. I need a way to delete all cookies from all places while the webbrowser is still open. Any help would be much appreciated.

BigPig89
  • 1
  • 1
  • Did you see this posting : http://stackoverflow.com/questions/7079565/delete-cookie-on-clicking-sign-out – jdweng May 25 '16 at 16:08
  • Yes I did now but I am not sure how HttpCookie works. Because when I added using System.web the intellisense still does not show it. Is there a reference I need to add in solution explorer to try this code? – BigPig89 May 25 '16 at 17:15
  • You need a Request. The solutions assumed you had a HttpWebRequest or WebRequest. – jdweng May 25 '16 at 21:17

0 Answers0