title: Display MediaWiki page in WebBrowser control with login
body: <p>I want to display a MediaWiki page in my desktop application using a .NET WebBrowser control. The wiki requires a login. The answer provided <a href="https://stackoverflow.com/questions/19521357/how-to-login-programmatically-to-mediawiki-website">here</a> is not acceptable for me because AutoComplete dialogs pop up when using the WebBrowser control.</p>
  
  <p>My approach is to login with a System.Net.WebClient object using api.php?action=clientlogin and then use the cookie that I get to access a wiki page. I can successfully login using the API but when I load a page I'm not logged in.</p>
  
  <p>The code below uses a WebClient object to load the final page. This is for testing purposes and to eliminate another source of error (changing the User-Agent for example).</p>
  
  <p>How do I do this correctly? Am I approaching this wrong?</p>
  
  <pre class="lang-cs prettyprint-override"><code>private void Login(string username, string password)
  {
      string cookie, loginToken, baseUri = "https://www.mediawiki.org";
      var client = new WebClient();
  
      // get logintoken and cookie
      using (var stream = client.OpenRead(baseUri + "/w/api.php?action=query&amp;meta=tokens&amp;format=xml&amp;type=login"))
      {
          cookie = client.ResponseHeaders[HttpResponseHeader.SetCookie].Split(';')[0];
  
          var document = System.Xml.Linq.XDocument.Load(stream);
          loginToken = (string)document.XPathEvaluate("string(/api/query/tokens/@logintoken)");
      }
  
      // login
      client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
      client.Headers[HttpRequestHeader.Cookie] = cookie;
  
      var response = client.UploadString(baseUri + "/w/api.php?format=xml", "action=clientlogin&amp;username=" + username +
                                         "&amp;password=" + password + "&amp;loginreturnurl=" + baseUri +
                                         "&amp;logintoken=" + Uri.EscapeDataString(loginToken));
  
      // new cookie (???)
      cookie = client.ResponseHeaders[HttpResponseHeader.SetCookie].Split(';')[0];
  
      // success?
      if (response.Contains("status=\"PASS\""))
      {
          client.Headers.Clear();
          client.Headers[HttpRequestHeader.Cookie] = cookie;
  
          var data = client.DownloadData(baseUri + "/wiki/API:Login");
          var text = Encoding.UTF8.GetString(data); // &lt;--- not logged in
      }
  }
  </code></pre>