Managing cookies in a WPF WebBrowser control?
Asked Answered
L

6

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);  
Lowrey answered 29/12, 2009 at 16:41 Comment(0)
T
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.

Trabue answered 29/12, 2009 at 19:46 Comment(1)
+1, I didn't know about these APIs... could prove useful someday !Celenacelene
D
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.

Demetri answered 28/6, 2012 at 14:54 Comment(2)
Does not answer the question of how to read and write cookies.Mcnew
@Mcnew 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.Demetri
T
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.

Trabue answered 29/12, 2009 at 19:46 Comment(1)
+1, I didn't know about these APIs... could prove useful someday !Celenacelene
B
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

Breana answered 12/4, 2011 at 10:34 Comment(0)
S
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.

Sea answered 17/6, 2014 at 15:49 Comment(2)
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].Antimony
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: referencesource.microsoft.com/#PresentationCore/src/Core/CSharp/…Sea
L
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/

Lengthwise answered 22/6, 2011 at 21:17 Comment(0)
P
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
            );
        }
Psychology answered 6/8, 2020 at 18:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.