我希望网站无法在webbrowser c#control应用程序中读取cookie或编写新的cookie.我希望在webbrowser c#应用程序运行时禁用所有网站的所有读/写cookie操作,如果没有,那么我有一个网站列表,其读/写cookie操作应该被禁用.
我使用的是.NET 2.0框架,但也可以使用4.5
解决方法:
您无法仅在Web浏览器控件上禁用Cookie.该控件本质上是一个嵌入式Internet Explorer,并共享用户的Internet Explorer设置.如果您不介意在Internet Explorer的所有其他实例上阻止cookie(可能您在浏览的其余部分使用Chrome或Firefox),则可以执行以下操作:
(来自:http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/90834f20-c89f-42f9-92a8-f67ccee3799a/)
To block Cookies in WebBrowser control you can take the following
steps, in fact, it’s the same as to block Cookies in IE.
- Choose “Internet Options” under the “Tools” menu on the IE;
- Select the “Privacy” tab.
- Click the “Advanced…” button in the “Settings” groupbox.
- Check the “Override automatic cookie handling” option.
- Check both “Block” options.
- Click “OK”
您也可以在访问页面后删除所有Cookie,但我认为这不会实现您完全匿名的目标.
我做了一点挖掘,我认为你可以使用InternetSetOption和INTERNET_SUPPRESS_COOKIE_PERSIST标志.根据文档,这仅适用于Internet Explorer 8及更高版本.
private const int INTERNET_OPTION_SUPPRESS_BEHAVIOR = 3; //INTERNET_SUPPRESS_COOKIE_PERSIST - Suppresses the persistence of cookies, even if the server has specified them as persistent.
[DllImport("wininet.dll", SetLastError = true)]
private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);
然后,当您初始化您的应用程序时尝试:
InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SUPPRESS_BEHAVIOR, IntPtr.Zero, 0);
希望这会让你走上正轨.也可以看看:
How to set and delete cookies from WebBrowser Control for arbitrary domains
How do I use InternetSetOption?
Clear Cookies Cache for Multiple WebBrowser Control with WinInet in Winform Application