WebBrowser control and cookies - vb.net

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.

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.

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

Related

Automatic login when run the program second time

Recently try to use a webbrowser component.
when I click button, it will navigate to facebook.com. after I login on first time and stop the program. and then when I run the program second time I don't need to fill the email and password. why I don't need to fill an email and password textbox ? thanks
Here's the code
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
WebBrowser1.Navigate("m.facebook.com")
End Sub
End Class
Note: I will delete this post, because it doesn't help the community. Just my curiosity. Thanks
how do i need to re-type the email password again when 2nd starting the program
You have two options:
The first is to save the CookieContainer associated with the WebBrowser to disk and reload it when you restart your program. There is no straightforward way to serialize cookies to disk or other storage method - that is up to you.
Instruct the WebBrowser object to make use of the user's Windows profile Internet Explorer cookie collection which is shared with Internet Explorer and other programs that also opt-in to sharing state (I do not know if this also applies to the Edge browser, I suspect it does not). See here for instructions on how to do this: Use cookies from CookieContainer in WebBrowser

Enable Tab Key in my Web Browser Control

I have a web browser control in a custom task pane control (User Control) and I open it as a sidebar as soon as my Outlook opens (I have created it as an Outlook Addin using Visual Studio 2013). The web browser control has a login form inside it and I would like to place focus on an input control in my web browser control as soon as Outlook opens. I have tried several tricks like placing focus on the custom user control and then placing focus on the input control after the form has loaded but it doesn't place focus on it. Also I have been trying to allow the use of Tab and Delete keys to work inside the web browser control so that I can tab to other controls and play with it like we would do it on a normal browser window. Kindly let me know how I can achieve this as I am out of ideas.
Cheers.
Try to use the Excel WebBrowser Control instead of the System.Windows.Forms WebBrowser; it handles special key forwarding as TAB, DEL, CTRL+V, etc.
For that change the WebBrowser contructor from
new System.Windows.Forms.WebBrowser();
to
new Microsoft.Office.Tools.Excel.Controls.WebBrowser();
You would need to add references to your project:
Project/Add Referernce/Extensions select
Microsoft.Tools.Outlook & Microsoft.Tools.Outlook.v4.0.Utilities
Doc: https://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.controls.webbrowser.aspx
You can only do that if you install a Windows hook (SetWindowsHookExW(WH_GETMESSAGE, YourHookProc, 0, GetCurrentThreadId()) and in the hook proc detect that messages are supposed to go to your browser, and redirect them accordingly.
It works partially this way but not 100%. First I had to set the TabStop property to True for my webbrowser control and then just set the focus once the document was loaded and that was it. The tab, delete, backspace keys all ran properly.
Private Sub CustomTaskPane_Load(sender As Object, e As EventArgs) Handles Me.Load
TestWebBrowser.Size = New Drawing.Size(Me.Width, Me.Height)
Me.Focus()
TestWebBrowser.Navigate(url)
WaitForPageLoad()
TestWebBrowser.TabIndex = 0
TestWebBrowser.TabStop = True
End Sub
Private Sub TestWebBrowser_DocumentCompleted(sender As Object, e As Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles TestWebBrowser.DocumentCompleted
' Now set the focus on the Web Browser Control
TestWebBrowser.Focus()
End Sub

Windows Mobile SDK : how to turn a label/button into a website link?

I am programming in Windows Mobile SDK 6 using Visual Basic, i would like to know how to make a button open the smartphone browser with a specific website path (make a link)
I got to the next piece of code so far:
Public Class GuitarHelperPage
Public link As New WebBrowser
Public adress As Uri
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
adress = New Uri("https://www.google.com")
Try
link.Navigate(adress)
'link.Focus()
Catch ex As System.UriFormatException
Return
End Try
End Sub
(the commented 'link.Focus()' its just something i tryed out, but i got the same output)
I simply want to click the button and open the browser at google homepage. But when I click it, the warning "This page contains both secure and nonsecure items. Do you want to continue?" appears and when I click "yes", nothing else hapens.
I've been researching and found this on MSDN help pages:
"The WebBrowser class can only be used in threads set to single thread apartment (STA) mode. To use this class, ensure that your Main method is marked with the STAThreadAttribute attribute"
SO i added this my main method:
<STAThread()> _
Shared Sub Main()
But still i get this error : "Type STAThread is not defined." And can't find a away around, I tried to add this same attribute to a Windows Desktop VB project and it works, maybe there is another way to do it in Mobile?
I am using .Net Framework 3.5 and windows mobile sdk 6.0 for this project
Please help, thank you.
If you want to open the default browser, you don't need a WebBrowser control. Just use the Process class, specifically the Start overload that takes in a ProcessStartInfo. Set the UseShellExecute property of the ProcessStartInfo to true so that it will open the default browser (e.g. if the user installed Opera Mobile, it will use that, not just always force IE).

Help files open in two browsers

In our application, when you go to "Menu > Help...", it opens our help files in Internet Explorer, which is the desirable effect. However, we have the shortcut key "F1" set to open the help files as well. It still opens the help in Internet Explorer... but it also opens them up at the same time in the user's default web browser!
Here is the code in the event method that opens the web browser:
Private Sub menuHelpHelp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuHelpHelp.Click
Dim temp As String = String.Format("{0}\Internet Explorer\iexplore.exe", Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles))
Dim temp2 As String = String.Format("{0}", Globals.HelpFilePath)
If Not System.IO.File.Exists(temp2)
MessageBox.Show(String.Format("Could not find index.html, run ""help_zip.exe"" inside the Doc folder as an Administrator to restore the help system (make sure to overwrite all files when asked!)"))
Else
System.Diagnostics.Process.Start(temp, temp2)
End If
End Sub
If I step through the code after clicking the Help menu item, IE opens as intended when I hit the "System.Diagnostics.Process.Start(temp, temp2)" line. However, when I do the same thing after pressing F1, it opens first in the user's default browser, and then in IE second. Any ideas? The code in this part of the application is written in VB.NET, and the application is a .NET 3.5 app.
Out of curiosity, have you tried binding the action to another button to see if the problem still exists? Switch it from F1 to F2, see if it still happens. Almost certainly will, unless there's some code in your program which you forgot about that's causing problems.
EDIT
You appear to be setting a global variable for your help path.. which means the default F1 Help would likely load the correct file. So I'd go with the "F1 is still bound to the default Help action." You'll need to unbind it.

How to determine URL about to be opened by the NewWindow event?

I'm trying to build a simple desktop application to keep users on a certain website. It's very simple, just a webbrowser control with right click disabled. However certain applications on our website require popup windows and those windows always open up a full browser window.
Private Sub WebBrowser1_NewWindow(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow
'What will the URL of this new window be?
Console.WriteLine("New Window maybe to " & WebBrowser1.Url.AbsoluteUri)
End Sub
Catching the newwindow event seems straight forward, but the event does not specify the URL that is being passed to the new window. I have seen some mention of a "NewWindow2" event, but vb.net does not appear to have anything like that.
How can I get the URL that is about to be opened in a new window?
The link is generated from a normal web application and not through a webbrowser.navigate call so it could be just about anything.
Thank you
You can catch the NewWindow2 event, create a new browser instance, host it in a new form or tab and pass it to ppDisp, then handle BeforeNavigate2 to get the new url. You can cancel the navigation if necessary. See Handle the BeforeNavigate2 event in the new window to get the url. for details.