WebBrowser control and cookies

我的未来我决定 提交于 2019-12-23 19:35:06

问题


I have a problem with WebBrowser control and cookies.

First of all what happens when one navigates in a normal Web browser (IE9 in my case):

1.1. I open Web page http://www.gmail.com.

I enter my username / password,
I leave the checkbox "Stay signed in" unchecked and click "Sign in",
IE9 opens my GMail page with all my mails listed. That is OK.

1.2. At the top of GMail page there are a lot of links like "Calendar", "Documents", etc.

When I click the "Documents" link, my documents page is opened in a separate tab in IE9. No additional login information as name / psw is asked. This is fine too.

Now, what happens when I repeat all that in WebBrowser control (I have created a very simple VB.NET application with single WebBrowser control in it).

2.1. In form load event the following code is executed:

Private Sub MyForm_Load(sender As System.Object, e As System.EventArgs)
    Me.MyWebBrowser.Navigate("http://www.gmail.com")
End Sub

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

2.3. When I click the "Documents" link, a new instance of IE9 is opened,

2.4. Instead of showing a list of my documents, Google asks me to login again in an IE9 window. Why? Why I have to enter my credentials again?

I think there is something wrong with cookies and they are not set correctly in a step 2.2. Do you have any ideas what is wrong and what must be done to get cookies set correctly?

Thanks in advance,
Sal

Additional info:

I have my 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
    Next
End Sub

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


回答1:


that is because the web browser control is opening your link in a separate IE9 window, right? If you open it or have it opened in another web browser control window in your Winforms program, or in the same window where you clicked the link, then it should work properly.

They are using in session cookies (in memory) to hold your login information, not the sort written to the hard drive, this is why when you run another process, the information (in memory session cookies) are not present or propagated to the new process.

So, to intercept link clicking and have it open in a wb window of your choice, you need to intercept the newwindow event, cancel navigation and renavigate using .navigate to the wb of your choice, if you need help with this let me know but there are plenty online.

Also, keep in mind that the web browser control uses IE7 by default, even though you have IE9 installed, this can be changed via the registry.




回答2:


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


来源:https://stackoverflow.com/questions/9627776/webbrowser-control-and-cookies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!