Clearing WebBrowser control's cookies for all sites WITHOUT clearing for IE itself

10,018

Solution 1

Is it necessary to have the cookies functional in IE at the same time? Is it possible to "restore" the original cookies back to IE after your program runs?

UPDATE:

Another idea:

I wonder what would happen if you ran your program under a different user account, via Impersonation. It's possible that it would store those cookies under a different Windows profile...

Solution 2

That comment at How to set and delete cookies from WebBrowser Control for arbitrary domains is a mistake. The WinInet cache folder setting is apparently not in IE settings but is a Shell folder setting, since IDocHostUIHandler2 can only customize settings stored in IE's registry key it is not useful for this task. I don't know a way to customize the folder location except to hook all WinInet APIs (and stuck with updating application to accommodate future WinInet APIs), which is not easy in C#. I will update the old post now.

Share:
10,018
Teekin
Author by

Teekin

Updated on June 15, 2022

Comments

  • Teekin
    Teekin almost 2 years

    EDIT: As far as I know, there is no solution to this problem, making it yet another testament to the fact that one should not use C#'s WebBrowser. We ended up with a warning sign at the start of our program, notifying the user that cookies will be cleared for IE.

    The short version of what I'm trying to do is in the title. Here's the long version.

    I have a bit of a complex problem which I'm sure I will receive a lot of guesses as a response to. In order to keep the well-intended but unfortunately useless guesses to a minimum, let me first mention that the solution to this problem is not simple, so simple suggestions will unfortunately not help at all, even though I appreciate the effort.

    The .NET WebBrowser component is fundamentally IE itself so solutions with any sorts of caveats will almost certainly not work. I need to do exactly what I'm trying to do, and even a seemingly minor caveat will defeat the purpose completely. At the risk of sounding arrogant, I need assistance from someone who really has in-depth knowledge about the .NET WebBrowser and/or WinInet and/or how to communicate with Windows's underlying system from C#... or how to encapsulate C++ code in C#.

    That said, I don't expect anyone to do this for me, and I've found some promising hints which are explained later in this question.

    But first... what I'm trying to achieve is this.

    I have a Windows.Forms component which contains a WebBrowser control. This control needs to:

    1. Clear ALL cookies for ALL websites.
    2. Visit several websites, one after another, and record cookies and handle them correctly. This part works fine already so I don't have any problems with this.
    3. Rinse and repeat... theoretically forever.

    Now, here's the real problem. I need to clear all those cookies (for any and all sites), but only for the WebBrowser control itself and NOT the cookies which IE proper uses. What's fundamentally wrong with this approach is of course the fact that the .NET WebBrowser control is IE. But I'm a stubborn young man and I insist on it being possible, or else! ;)

    Here's where I'm stuck at the moment.

    It is quite simply impossible to clear all cookies for the WebBrowser control programmatically through C# alone. One must use DllImport and all the crazy stuff that comes with it. This chunk works fine for that purpose:

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

    And then, in the function that actually does the clearing of the cookies:

    InternetSetOption(IntPtr.Zero, INTERNET_OPTION_END_BROWSER_SESSION, IntPtr.Zero, 0);
    

    Then all the cookies get cleared and as such, I'm happy. The program works exactly as intended, aside from the fact that it also clears IE's cookies, which must not be allowed to happen.

    From one fellow StackOverflower (if that's a word), Sheng Jiang proposed this to a different problem in a comment, but didn't elaborate further:

    "If you want to isolate your application's cookies you need to override the Cache directory registry setting via IDocHostUIHandler2::GetOverrideKeyPath"

    I've looked around the internet for IDocHostUIHandler2 and GetOverrideKeyPath, but I've got no idea of how to use them from C# to isolate cookies to my WebBrowser control. My experience with the Windows registry is limited to RegEdit (so I understand that it's a tree structure with different data types but that's about it... I have no in-depth knowledge of the registry's relationship with IE, for example).

    Here's what I dug up on MSDN:

    IDocHostUIHandler2 docs: http://msdn.microsoft.com/en-us/library/aa753275%28VS.85%29.aspx

    GetOverrideKeyPath docs: http://msdn.microsoft.com/en-us/library/aa753274%28VS.85%29.aspx

    I think I know roughly what these things do, I just don't know how to use them.

  • Teekin
    Teekin about 14 years
    Hmmm... not crazy ideas at all! What I don't like about the former idea is that this program is intended for users... and we all know what those are like. The impersonation idea is also interesting, although I'm betting that then I'd run into problems with clients on Windows XP and before... but I'll check those out, thanks!
  • Admin
    Admin about 7 years
    If someone with 10 rank posted this, as an answer, they'd surely be ridiculed.