清除所有站点的WebBrowser控件的cookie,而不清除IE本身

时间:2022-01-29 23:12:07

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.

编辑:据我所知,没有解决这个问题的方法,这再次证明了一个人不应该使用C#的WebBrowser。我们在程序开始时结束了警告标志,通知用户将为IE清除cookie。

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

.NET WebBrowser组件基本上是IE本身,因此具有任何类型警告的解决方案几乎肯定不会起作用。我需要做的正是我正在做的事情,即使是一个看似微不足道的警告也会彻底击败目的。冒着听起来很傲慢的风险,我需要那些真正深入了解.NET WebBrowser和/或WinInet和/或如何通过C#与Windows底层系统进行通信的人的帮助......或者如何封装C ++代码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:

我有一个包含WebBrowser控件的Windows.Forms组件。此控件需要:

  1. Clear ALL cookies for ALL websites.
  2. 清除所有网站的所有Cookie。

  3. 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.
  4. 一个接一个地访问几个网站,并记录cookie并正确处理它们。这部分工作正常,所以我没有任何问题。

  5. Rinse and repeat... theoretically forever.
  6. 冲洗并重复...理论上永远。

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! ;)

现在,这是真正的问题。我需要清除所有这些cookie(对于任何和所有站点),但仅限于WebBrowser控件本身,而不是IE正确使用的cookie。这种方法的根本原因当然是.NET WebBrowser控件是IE。但我是一个固执的年轻人,我坚持认为这是可能的,否则! ;)

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:

仅通过C#以编程方式清除WebBrowser控件的所有cookie是完全不可能的。必须使用DllImport和它附带的所有疯狂的东西。这个块可以正常用于此目的:

[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:

然后,在实际清除cookie的功能中:

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.

然后所有的cookie都被清除了,因此,我很高兴。该程序完全按照预期工作,除了它还清除IE的cookie,这是不允许发生的。

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

来自*er的一位同事(如果只是一句话),盛江在评论中提出了一个不同的问题,但没有进一步阐述:

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

“如果要隔离应用程序的cookie,则需要通过IDocHostUIHandler2 :: GetOverrideKeyPath覆盖Cache目录注册表设置”

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

我在互联网上浏览了IDocHostUIHandler2和GetOverrideKeyPath,但我不知道如何使用它们从C#中将cookie与我的WebBrowser控件隔离开来。我对Windows注册表的体验仅限于RegEdit(所以我理解它是一个具有不同数据类型的树结构,但就此而言......例如,我没有深入了解注册表与IE的关系)。

Here's what I dug up on MSDN:

这是我在MSDN上挖掘的内容:

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

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

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.

我想我大致知道这些事情的作用,我只是不知道如何使用它们。

3 个解决方案

#1


4  

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?

是否有必要在IE中同时使用cookie?程序运行后,是否可以将原始cookie“恢复”回IE?

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

我想知道如果你通过Impersonation在不同的用户帐户下运行你的程序会发生什么。它可能会将这些cookie存储在不同的Windows配置文件中......

#2


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.

如何在任意域中设置和删除WebBrowser Control中的cookie是一个错误。 WinInet缓存文件夹设置显然不在IE设置中,但是是Shell文件夹设置,因为IDocHostUIHandler2只能自定义存储在IE注册表项中的设置,这对此任务没有用。我不知道自定义文件夹位置的方法,除了挂钩所有WinInet API(并坚持更新应用程序以适应未来的WinInet API),这在C#中并不容易。我现在将更新旧帖子。

#3


0  

http://wiki.imacros.net/iimInit%28%29#Separate_Browser_Instances explains a solution. Basically iMacros uses the IE InPrivate mode.

http://wiki.imacros.net/iimInit%28%29#Separate_Browser_Instances解释了一个解决方案。基本上iMacros使用IE InPrivate模式。

#1


4  

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?

是否有必要在IE中同时使用cookie?程序运行后,是否可以将原始cookie“恢复”回IE?

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

我想知道如果你通过Impersonation在不同的用户帐户下运行你的程序会发生什么。它可能会将这些cookie存储在不同的Windows配置文件中......

#2


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.

如何在任意域中设置和删除WebBrowser Control中的cookie是一个错误。 WinInet缓存文件夹设置显然不在IE设置中,但是是Shell文件夹设置,因为IDocHostUIHandler2只能自定义存储在IE注册表项中的设置,这对此任务没有用。我不知道自定义文件夹位置的方法,除了挂钩所有WinInet API(并坚持更新应用程序以适应未来的WinInet API),这在C#中并不容易。我现在将更新旧帖子。

#3


0  

http://wiki.imacros.net/iimInit%28%29#Separate_Browser_Instances explains a solution. Basically iMacros uses the IE InPrivate mode.

http://wiki.imacros.net/iimInit%28%29#Separate_Browser_Instances解释了一个解决方案。基本上iMacros使用IE InPrivate模式。