Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 797
EBAG Avatar asked Oct 03 '22 06:10

EBAG


1 Answers

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 !>?");
    }
}
like image 108
Darin Dimitrov Avatar answered Oct 12 '22 11:10

Darin Dimitrov