Managing cookies in a WPF WebBrowser control?

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

I'm 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); 
+7
browser cookies wpf cookiecontainer
source share
5 answers

the webbrowser control uses WinInet to work on the network, in particular, it uses the functions InternetSetCookie (Ex) and InternetGetCookie (Ex) to control the cookie. There is no WinInet shell in .Net, but you can p-invoke.

+6
source share

One of the potentially confusing WebBrowser controls and cookies is that at first glance it often looks like your application receives a separate cookie store. For example, if you go to a site where a permanent cookie is stored to identify you, then regardless of whether you register on this site inside the application that hosts the control, it will be regardless of how you logged in through Internet Explorer.

In fact, you can even log in with different identifiers.

However, although it would be natural to conclude that every application that accepts WebBrowser therefore receives its own cookies, this is actually not the case. There are only two sets of cookies: those that are used in "low integrity" mode (by default, this is IE), and the other set is what you get in a regular application with the WebBrowser host and what you get if you launch IE.

+6
source share

Yes, you're right, InternetGetCookieEx is the only way to get HttpOnly cookies, and this is the preferred way to capture a cookie using the WebBrowser control.

I posted a complete example here

+3
source share

You can use Application.GetCookie and Application.SetCookie .

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

+2
source share

I ran into the same problem a few days ago. Besides the examples of previous answers, here is the Win32 shell for the WebBrowser control. The advantage of this implementation is that it provides more options that the default WebBrowser control controls.

Unfortunately, if this is not native WPF, you will have to create a shell if you plan to use it in WPF.

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

0
source share

All Articles