title: WebBrowser control and cookies
body: <p>I have a problem with WebBrowser control and cookies.</p>
  
  <p>First of all what happens when one navigates in a normal Web browser (IE9 in my case):</p>
  
  <p>1.1. I open Web page <a href="http://www.gmail.com" rel="nofollow">http://www.gmail.com</a>.</p>
  
  <p>I enter my username / password,<br>
  I leave the checkbox "<em>Stay signed in</em>" <strong>unchecked</strong> and click "<em>Sign in</em>",<br>
  IE9 opens my GMail page with all my mails listed. That is OK.</p>
  
  <p>1.2. At the top of GMail page there are a lot of links like "Calendar", "Documents", etc.</p>
  
  <p>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.</p>
  
  <p>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).</p>
  
  <p>2.1. In form load event the following code is executed:</p>
  
  <p><code>
  Private Sub MyForm_Load(sender As System.Object, e As System.EventArgs)<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Me.MyWebBrowser.Navigate("http://www.gmail.com")<br>
  End Sub
  </code></p>
  
  <p>2.2. I enter my GMail login information (name and psw) in WebBrowser control,</p>
  
  <p>2.3. When I click the "Documents" link, a new instance of IE9 is opened,</p>
  
  <p>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?</p>
  
  <p>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?</p>
  
  <p>Thanks in advance,<br>
  Sal</p>
  
  <p>Additional info:</p>
  
  <p>I have my WebBrowser NewWindow event function implemented as:</p>
  
  <pre><code>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 &lt;&gt; -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
  </code></pre>
  
  <p>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.</p>