Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 885
Teekin Avatar asked Mar 11 '10 20:03

Teekin


People also ask

What happens if I clear all my cookies?

Clear all cookies If you remove cookies, you'll be signed out of websites and your saved preferences could be deleted. Settings. Clear browsing data. Choose a time range, like Last hour or All time.

Does clearing cookies log out of websites?

Clear all cookies Important: If you remove cookies, you're signed out of websites and your saved preferences could be deleted. Cookies and other site data. Clear all data.

Can I clear my cookies for just one site?

Select the lock icon next to a website name in the Address bar, then select Cookies. In the Cookies in use dialog box, expand a site name, choose a cookie, then select Remove.

How often should you clear your browser cookies?

So how often should you clean these cookies? If you're using a public computer, you should delete them and other data, such as browsing history, right after your session. If it's your personal device, it's a good idea to remove all cookies at least once a month to keep your device neat.


2 Answers

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...

like image 191
Dave Swersky Avatar answered Sep 23 '22 17:09

Dave Swersky


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.

like image 3
Sheng Jiang 蒋晟 Avatar answered Sep 24 '22 17:09

Sheng Jiang 蒋晟