2013-08-12 5 views
5

Poiché WebBrowser in C# condivide i cookie con tutte le altre istanze di WebBrowser, tra cui IE, vorrei che un browser Web abbia il proprio contenitore dei cookie che non condivide alcun cookie è stato creato precedentemente in IE o in altre istanze.Make .NET WebBrowser per non condividere i cookie con IE o altre istanze

Così, ad esempio, quando creo un browser web non dovrebbe avere alcun cookie. E quando eseguo 2 istanze di WebBrowser hanno il proprio contenitore di cookie e non condividono o non confliggono i cookie l'uno con l'altro.

Come posso ottenere questo risultato?

risposta

3

Si potrebbe fare questo per ogni processo utilizzando la funzione 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); 

e poi a vostra all'avvio dell'applicazione chiamare la seguente funzione:

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

E 'possibile fare questo per thread? – EBAG

+0

Io non la penso così. Dopo tutto non dimenticare che il controllo WebBrowser che stai utilizzando è solo un wrapper sull'oggetto COM nativo che rappresenta Internet Explorer. –

+0

Per quanto riguarda i cookie di condivisione, non penso sia possibile nemmeno su base per-thread. La sessione è condivisa per processo per le istanze di WebBrowser. Ecco una [domanda] simile (http://stackoverflow.com/questions/18055734/net-webbrowser-control-and-dispose/18057266#18057266). – Noseratio

Problemi correlati