Web browser control and cookies

I have a problem with managing WebBrowser and cookies.

First of all, what happens when you switch to a regular web browser (IE9 in my case):

1.1. I open the web page http://www.gmail.com .

I enter my username / password,
I leave the "Stay signed in" checkbox unchecked and click "Login",
IE9 opens my GMail page with all my emails. This is normal.

1.2. There are many links at the top of the GMail page such as Calendar, Documents, etc.

When I click the Documents link, my Documents page opens in a separate tab in IE9. There is no additional login information as specified by the name / psw. It's also good.

Now what happens when I iterate all of this in a WebBrowser control (I created a very simple VB.NET application with one WebBrowser control in it).

2.1. The following code is executed in the form load event:

Private Sub MyForm_Load(sender As System.Object, e As System.EventArgs)
End Sub

2.2. I enter my GMail login details (name and psw) in the WebBrowser control,

2.3. When I click the Documents link, a new instance of IE9 opens,

2.4. Instead of showing a list of my documents, Google asks me to log into the IE9 window again. What for? Why do I need to enter my credentials again?

I think there is something wrong with cookies and they are not set correctly in step 2.2. Do you have any idea what is wrong and what needs to be done to set the cookie correctly?

Thanks in advance,

Additional Information:

I have a WebBrowser NewWindow event function implemented as:

Private Sub MyWebBrowser_NewWindow(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles MyWebBrowser.NewWindow
    Dim CookiesArr As String() = MyWebBrowser.Document.Cookie.Split(";")
    For Each Cookie In CookiesArr
        Dim Idx As Long = Cookie.IndexOf("=")
        If Idx <> -1 Then
            Dim CookieName As String = Cookie.Substring(0, Idx).Trim
            Dim CookieValue As String = Cookie.Substring(Idx + 1).Trim
            InternetSetCookie("http://www.google.com", Nothing, CookieName + " = " + CookieValue + "; expires = Sat,05-Jan-2013 00:00:00 GMT")
        End If
End Sub


I believe the InternetSetCookie () method should store cookies permanently in the "C: \ Users \ Administrator \ AppData \ Roaming \ Microsoft \ Windows \ Cookies" directory for reuse when a Google page is opened that requires authorization.


source to share

2 answers

because the web browser control opens your link in a separate IE9 window, right? If you open it or open it in another web browser control window in your Winforms program or in the same window where you clicked the link, then it should work correctly.

They use session cookies (in memory) to store your login information, not a write written to the hard drive, so when you start another process, the information (in session memory cubes) is missing or not propagated to the new process.

So, to intercept a link and open it in a wb window of your choice, you need to intercept the newwindow event, cancel navigation and rename with .navigate to the wb of your choice, if you need help with this let I know but there are many online.

Also, keep in mind that IE7 is the default web browser control, even if you have IE9 installed, this can be changed through the registry.



Try adding this:

Private Declare Function InternetSetCookie Lib "wininet.dll" Alias "InternetSetCookieA" (ByVal lpszUrlName As String, ByVal lpszCookieName As String, ByVal lpszCookieData As String) As Boolean




All Articles