8

Is there a way to read/write the cookies that a WebBrowser control uses?

I am doing something like this...

string resultHtml;
HttpWebRequest request = CreateMyHttpWebRequest(); // fills http headers and stuff
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
    resultHtml = sr.ReadToEnd();
}
WebBrowser browser = new WebBrowser();
browser.CookieContainer = request.CookieContainer; // i wish i could do this :(
browser.NavigateToString(resultHtml);  
kenwarner
  • 28,650
  • 28
  • 130
  • 173

6 Answers6

7

One of the potentially confusing things about the WebBrowser control and cookies is that at a first glance, it often looks like your app gets a separate cookie store. For example, if you log into a site that stores a persistent cookie to identify you, then whether you appear to be logged in for that site from inside an app hosting the control will be independent of whether you seem to be logged in via Internet Explorer.

In fact, you can even be logged in with different identities.

However, although it might be natural to draw the conclusion that each app hosting the WebBrowser therefore gets its own cookies, in fact that's not true. There are merely two sets of cookies: the ones used in 'low integrity' mode (which is what IE runs in by default), and the other set, which is what you'll get in a normal app that hosts the WebBrowser and also what you'll get if you run IE elevated.

Ian Griffiths
  • 14,302
  • 2
  • 64
  • 88
  • 4
    Does not answer the question of how to read and write cookies. – Kurren Mar 16 '15 at 14:44
  • 1
    @Kurren but it does address what the op was trying to do - look at the source code and it is clear that the goal was to change ('write') or discover ('read') which particular set of cookies the browser was using. Moreover, my answer explains that WebBrowser uses the same cookie stores IE always uses, which tells you that there's nothing special about how to access the cookies WebBrowser uses - you do it the same way you always would. And since Sheng Jiang already showed how to do that, there would be no point in me repeating it; my goal was to provide useful additional information. – Ian Griffiths Mar 18 '15 at 15:58
6

the webbrowser control uses WinInet for networking, specifically use the InternetSetCookie(Ex) and InternetGetCookie(Ex) functions for Cookie management. There isn't a WinInet wrapper in .Net, but you can p-invoke.

Sheng Jiang 蒋晟
  • 15,125
  • 2
  • 28
  • 46
3

You can use Application.GetCookie and Application.SetCookie methods.

Although Application is more or less related to WPF, you can use these methods in any desktop .NET code. In fact, they are wrappers on InternetGetCookieEx and InternetSetCookieEx Windows APIs.

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
  • I tried Application.GetCookie and all I got was NO_MORE_ITEMS. When I used the sample code from Alkampfer that used InternetGetCookieEx directly it returned the cookies properly. I think there must be different parameters sent by the Application routine that keep it from getting my cookies. [Session only], [Secure only], [Http Only]. – Prof Von Lemongargle Jul 31 '14 at 22:13
  • Yes, alkampfer's code uses the INTERNET_COOKIE_HTTPONLY flag, while Application.GetCookie doesn't. That may be the difference. You can see the code here: http://referencesource.microsoft.com/#PresentationCore/src/Core/CSharp/MS/Internal/AppModel/CookieHandler.cs – Simon Mourier Aug 01 '14 at 06:19
3

Yes you are right, InternetGetCookieEx is the only way to retrieve HttpOnly cookies and it is the preferred way to grab cookie from WebBrowser control.

I posted a complete example here

Community
  • 1
  • 1
Alkampfer
  • 1,359
  • 12
  • 27
0
 Here is sample from [link][1]
 >   public static class WinInetHelper
        {
            public static bool SupressCookiePersist()
            {
                // 3 = INTERNET_SUPPRESS_COOKIE_PERSIST
                // 81 = INTERNET_OPTION_SUPPRESS_BEHAVIOR
                return SetOption(81, 3);
            }
    
            public static bool EndBrowserSession()
            {
                // 42 = INTERNET_OPTION_END_BROWSER_SESSION
                return SetOption(42, null);
            }
            static bool SetOption(int settingCode, int? option)
            {
                IntPtr optionPtr = IntPtr.Zero;
                int size = 0;
                if (option.HasValue)
                {
                    size = sizeof(int);
                    optionPtr = Marshal.AllocCoTaskMem(size);
                    Marshal.WriteInt32(optionPtr, option.Value);
                }
    
                bool success = InternetSetOption(0, settingCode, optionPtr, size);
    
                if (optionPtr != IntPtr.Zero) Marshal.Release(optionPtr);
                return success;
            }
    
            [System.Runtime.InteropServices.DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
            private static extern bool InternetSetOption(
            int hInternet,
            int dwOption,
            IntPtr lpBuffer,
            int dwBufferLength
            );
        }
Taran
  • 2,895
  • 25
  • 22
0

I faced the same issue few days ago. Besides the examples of the previous answers, here is a Win32 wrapper for the WebBrowser control. The advantage of this implementation is that it exposes more options that the default WebBrowser control.

Unfortunately if It's not WPF native, so you will have to create a wrapper if you're planning to use it in WPF.

http://code.google.com/p/csexwb2/

hmadrigal
  • 1,020
  • 11
  • 19