5

由于 C# 中的 WebBrowser 与包括 IE 在内的所有其他 WebBrowser 实例共享 cookie,我希望 WebBrowser 拥有自己的 cookie 容器,该容器不共享以前在 IE 或其他实例中创建的任何 cookie。

因此,例如,当我创建一个 WebBrowser 时,它不应该有任何 cookie。当我运行 2 个 WebBrowser 实例时,它们有自己的 cookie 容器,并且不会相互共享或冲突 cookie。

我怎样才能做到这一点?

4

1 回答 1

4

InternetSetOption您可以使用Win32 函数为每个进程执行此操作:

[DllImport("wininet.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
public static extern bool InternetSetOption(int hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);

然后在您的应用程序启动时调用以下函数:

private unsafe void SuppressWininetBehavior()
{
    /* SOURCE: http://msdn.microsoft.com/en-us/library/windows/desktop/aa385328%28v=vs.85%29.aspx
    * INTERNET_OPTION_SUPPRESS_BEHAVIOR (81):
    *      A general purpose option that is used to suppress behaviors on a process-wide basis. 
    *      The lpBuffer parameter of the function must be a pointer to a DWORD containing the specific behavior to suppress. 
    *      This option cannot be queried with InternetQueryOption. 
    *      
    * INTERNET_SUPPRESS_COOKIE_PERSIST (3):
    *      Suppresses the persistence of cookies, even if the server has specified them as persistent.
    *      Version:  Requires Internet Explorer 8.0 or later.
    */


    int option = (int)3/* INTERNET_SUPPRESS_COOKIE_PERSIST*/;
    int* optionPtr = &option;

    bool success = InternetSetOption(0, 81/*INTERNET_OPTION_SUPPRESS_BEHAVIOR*/, new IntPtr(optionPtr), sizeof(int));
    if (!success)
    {
        MessageBox.Show("Something went wrong !>?");
    }
}
于 2013-08-12T20:20:06.690 回答