Make .NET WebBrowser not to share cookies with IE or other Instances

前提是你 提交于 2019-12-18 13:31:44

问题


Since WebBrowser in C# shares cookies with all other instances of WebBrowsers including IE I would like for a WebBrowser to have it's own cookie container that doesn't share any cookies that was created previously in IE or other instances.

So for example when I create a WebBrowser it shouldn't have any cookies. And when I run 2 instances of WebBrowsers they have their own cookie container and don't share or conflict cookies with each other.

How can I achieve this ?


回答1:


You could do this per process using the InternetSetOption Win32 function:

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

and then at your application startup call the following function:

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 !>?");
    }
}


来源:https://stackoverflow.com/questions/18195844/make-net-webbrowser-not-to-share-cookies-with-ie-or-other-instances

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