5

Can you say how I can delete all cookies in private System.Windows.Forms.WebBrowser webBrowser1. I used

[DllImport("wininet.dll", SetLastError = true)]
private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);

I want do this with button click.

private void button1_Click(object sender, EventArgs e)
{
         InternetSetOption(IntPtr.Zero, INTERNET_OPTION_END_BROWSER_SESSION, IntPtr.Zero, 0);
} 

But it doesn't work. And I know about webBrowser1.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());}}}})())"); It isn't suitable for me.

noseratio
  • 59,932
  • 34
  • 208
  • 486
jenius
  • 257
  • 1
  • 4
  • 17
  • This may work but it's undocumented: http://stackoverflow.com/q/17369275/1768303. This may also help, depending on your goals: http://stackoverflow.com/a/21254020/1768303. – noseratio Jan 22 '14 at 02:12
  • 1
    I do not quite understand where I should put dynamic `document=webBroweser.Document; document.execCommand("ClearAuthenticationCache", false);` Inside my button?? I get Microsoft.CSharp.RuntimeBinder.RuntimeBinderException in System.Core.dll – jenius Jan 22 '14 at 10:01
  • 1
    System.Windows.Forms.HtmlDocument does not contain a definition for "execCommand" – jenius Jan 22 '14 at 10:01
  • You forgot `dynamic` keyword: `dynamic document=webBroweser.Document;`. There is no *hard-typed* `execCommand` method, unless you use MSHTML PIA. – noseratio Jan 22 '14 at 10:24
  • 1
    I forgot it only in this comment. – jenius Jan 22 '14 at 11:00
  • I can't tell why you're seeing this error. Is `webBrowser.Document == null`? Does `webBrowser.Document.Body.OuterHtml` throw too? – noseratio Jan 22 '14 at 11:10
  • I understand, it can't help. – jenius Jan 22 '14 at 14:27

1 Answers1

5

the correct typing is as follows:

dynamic document = webBrowser1.Document;
document.ExecCommand("ClearAuthenticationCache", false, null);

Cheers.

stack user
  • 63
  • 4