2013-08-12 27 views
5

Vì WebBrowser trong C# chia sẻ cookie với tất cả các trường hợp khác của WebBrowsers bao gồm IE Tôi muốn một WebBrowser có vùng chứa cookie riêng không chia sẻ bất kỳ cookie nào đã được tạo trước đó trong IE hoặc các phiên bản khác.Làm cho WebBrowser .NET không chia sẻ cookie với IE hoặc các Trường hợp khác

Vì vậy, ví dụ: khi tôi tạo WebBrowser, nó sẽ không có bất kỳ cookie nào. Và khi tôi chạy 2 trường hợp WebBrowsers, họ có vùng chứa cookie của riêng mình và không chia sẻ hoặc xung đột cookie với nhau.

Tôi làm cách nào để đạt được điều này?

Trả lời

3

Bạn có thể làm điều này cho mỗi quá trình sử dụng InternetSetOption Win32 chức năng:

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

và sau đó lúc khởi động ứng dụng của bạn gọi hàm sau:

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

Có thể làm điều này cho mỗi thread? – EBAG

+0

Tôi không nghĩ vậy. Sau khi tất cả không quên rằng điều khiển WebBrowser mà bạn đang sử dụng chỉ là một wrapper trên đối tượng COM gốc đại diện cho Internet Explorer. –

+0

Về việc chia sẻ cookie, tôi không nghĩ rằng nó có thể thậm chí trên cơ sở từng luồng. Phiên được chia sẻ cho mỗi quá trình cho các cá thể WebBrowser. Đây là một [câu hỏi] tương tự (http://stackoverflow.com/questions/18055734/net-webbrowser-control-and-dispose/18057266#18057266). – Noseratio