Disable Cookie read/write in webbrowser c# application

南楼画角 提交于 2019-12-05 13:28:48
JoshVarty

You can't disable cookies only on your web browser control. The control is essentially an embedded Internet Explorer and shares the user's Internet Explorer settings. If you don't mind blocking cookies on all other instances of Internet Explorer (maybe you use Chrome or Firefox for the rest of your browsing) you can do the following:

(From: 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.

  1. Choose "Internet Options" under the "Tools" menu on the IE;
  2. Select the "Privacy" tab.
  3. Click the "Advanced..." button in the "Settings" groupbox.
  4. Check the "Override automatic cookie handling" option.
  5. Check both "Block" options.
  6. Click "OK"

You could also delete all the cookies after you visit a page, but I don't think this will fulfill your goal of being completely anonymous.

I did a little digging and I think you can use InternetSetOption and the INTERNET_SUPPRESS_COOKIE_PERSIST flag. According to the documentation, this will only work for Internet Explorer 8 and later.

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

Then when you initialize your app try:

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

Hopefully this puts you on the right track. See also:

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

You can use InternetSetOption with the option flag INTERNET_OPTION_SUPPRESS_BEHAVIOR. This option flag should be used together with INTERNET_SUPPRESS_COOKIE_PERSIST option.

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

    public static void SuppressCookiePersist()
    {
        int dwOption = 81; //INTERNET_OPTION_SUPPRESS_BEHAVIOR
        int option = 3; // INTERNET_SUPPRESS_COOKIE_PERSIST

        IntPtr optionPtr = Marshal.AllocHGlobal(sizeof(int));
        Marshal.WriteInt32(optionPtr, option);

        InternetSetOption(IntPtr.Zero, dwOption, optionPtr, sizeof(int));
        Marshal.FreeHGlobal(optionPtr);
    }
}

The setting is valid per process, so the method can be called at any place, but before webBrowser.Navigate().

Note:
- Requires Internet Explorer 8.0 or later.
- To reset use int option = 4; //INTERNET_SUPPRESS_COOKIE_PERSIST_RESET

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!