Disable Cookie read/write in webbrowser c# applica

2019-05-07 13:30发布

问题:

I want websites to not able to read cookies or write new cookies in a webbrowser c# control application. I will prefer to disable all read/write cookies operation for all websites when the webbrowser c# application runs, if not then I have a list of websites whose read/write cookies operation should be disabled.

I am using .NET 2.0 framework but can also use 4.5

回答1:

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



回答2:

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