views:

5423

answers:

4

I am working with the Webbrowser control on a windows.form application written in C#. I would like to write a method for deleting the cookies from the Webbrowers control after it visits a certain site. Unfortunately, I don't know how to do that exactly and haven't found alot of help on the internet.

If anyone has experience actually doing this, not just hypothetics because it might be tricker than it seems, i don't know.

int count = webBrowser2.Document.Cookie.Length;
        webBrowser2.Document.Cookie.Remove(0,count);

I would just assume something like the above code would work but I guess it won't. Can anyone shed some light on this whole cookie thing?

+1  A: 

Does the webbrowser control show pages form mutliple sites that you, as a developer, are not in control of, or are you just using the web browser control to view custom HTML pages created within your application?

If it is the former, cookies are directly tied to the domain that sets them, and as such to delete these cookies you would need to monitor the users's cookie directory and delete any new cookies created, a track changes to existing cookies.

If it is the later, you can always send the webbrowser control to a custom page that deletes the cookies with either server-side scripting (if available in your application) or JavaScript.

Goblyn27
I guess the WebBrowser control doesn't use something like a CookieContainer object that we can just reset to reset all cookies, eh?
marduk
Well, I read somewhere on google that webbrower control cookies are stored in some application cache seperate from IE. I don't mind deleting all the cookies from the cookies folder in windows but I'm just not sure if that will solve the problem.
Proximo
Hmmm, that could be the case currently. The last time I used a WebBrowser Control was with VB6 of all things, so they certainly may have refined that by now. I wouldnt delete all the users cookies though, in any case. Thats just mean. I stored my passwords to naughty sites in those cookies. =)
Goblyn27
Great, well let me test your suggestion out and see if it will work, if so I'll give you the check.
Proximo
+2  A: 

If you have JavaScript enabled you can just use this code snippet to clear to clear the cookies for the site the webbrowser is currently on.

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());}}}})())")

It's derived from this bookmarklet for clearing cookies.

Jordan Milne
Sweet! This does the job
Proximo
Just as a side note, if you need to be able to clear cookies for all sites, you're probably better off using something like the axWebBrowser COM object. .NET's built in WebBrowser control is pretty locked down. I only used this when switching to axWebBrowser wasn't an option and the browser only needed to be able to browse one site.
Jordan Milne
+1  A: 
Jack
Looks cut off but thanks for the idea
Proximo
A: 

I'm using this code and it works without JavaScript. It's doing the same things as the JavaScript, but in VB.NET. Also, no navigation needed.

For Each cookie As String In Document.Cookie.Split(";"c)
    Dim domain As String = "." + url.Host
    While domain.Length > 0
        Dim path As String = url.LocalPath
        While path.Length > 0
            Document.Cookie = cookie.Split("="c)(0).Trim & "= ;expires=Thu, 30-Oct-1980 16:00:00 GMT;path=" & path & ";domain=" & domain
            path = path.Substring(0, path.Length - 1)
        End While
        Select Case domain.IndexOf(".")
            Case 0
                domain = domain.Substring(1)
            Case -1
                domain = ""
            Case Else
                domain = domain.Substring(domain.IndexOf("."))
        End Select
    End While
Next

The only real difference from the JavaScript is where, instead of just expiring cookie=value, I specifically search for the = and expire cookie=. This is important for expiring cookies that have no value.

Pitfalls:

  • You can only delete the cookies of the website to which you have navigated.
  • If the page is redirected to a different domain, the cookies you remove might be from the redirected domain. Or not, it's a race between the redirect and your code.
  • Don't access Document until ReadyState = WebBrowserReadyState.Complete, otherwise Document will be Nothing and the dereference will throw an exception.
  • Probably lots of others, but this code works great for me.

Firefox with the View Cookies Add-On helped a lot in debugging specific web pages.

Eyal