title: How to use c# login wordpress
body: <p>This example login function <strong>But does not work for loading the Dashboard page</strong> wordpress. 
  I need to perform some action in wordpress admin panel programmatically but can't manage how to login to Wordpress using C# and HttpWebRequest.</p>
  
  <p>Here is what I do:</p>
  
  <pre><code>namespace Wordpress_Login
  {
      public class WPWorkerTests
      {
          CookieContainer cookieJar = new CookieContainer();
          public String StartAdmin()
          {
             //string wp_url = "http://localhost/wp";
             string admin_url = "http://localhost/wp/wp-admin";
             string login_url = "http://localhost/wp/wp-login.php";
             string post_data = "log=test&amp;pwd=asdfz&amp;rememberme=forever&amp;wp-submit=Log+In&amp;redirect_to=http%3A%2F%2Flocalhost%2Fwp%2Fwp-admin%2F&amp;testcookie=1";
  
             HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create(login_url);
             request1.CookieContainer = cookieJar;
             request1.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0";
             request1.KeepAlive = true; //this is the default
             request1.ContentType = "text/html";
             request1.Timeout = 10000;
             request1.Method = "GET";
             HttpWebResponse response1 = (HttpWebResponse)request1.GetResponse();
             StreamReader sr = new StreamReader(response1.GetResponseStream(), Encoding.ASCII);
             //MessageBox.Show(sr.ReadToEnd());
  
             HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create(login_url);
             request2.Method = "POST";
             request2.CookieContainer = cookieJar;
             request2.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0";
             request2.KeepAlive = true;
             request2.Timeout = 100000;
             request2.AllowAutoRedirect = false;
             request2.ContentType = "application/x-www-form-urlencoded";
             byte[] byteArray2 = Encoding.ASCII.GetBytes(post_data);
             request2.ContentLength = byteArray2.Length;
             Stream dataStream2 = request2.GetRequestStream();
             dataStream2.Write(byteArray2, 0, byteArray2.Length);
             dataStream2.Close();
             HttpWebResponse response2 = (HttpWebResponse)request2.GetResponse();
             //MessageBox.Show(((HttpWebResponse)response2).StatusDescription);
             dataStream2 = response2.GetResponseStream();
             StreamReader reader2 = new StreamReader(dataStream2);
             string report2 = reader2.ReadToEnd();
             //MessageBox.Show(report2);
             reader2.Close();
             dataStream2.Close();
             response2.Close();
  
             HttpWebRequest request3 = (HttpWebRequest)WebRequest.Create(admin_url);
             request3.Method = "GET";
             request3.CookieContainer = cookieJar;
             request3.ContentType = "text/html";
             request3.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0";
             request3.KeepAlive = true;
             request3.Timeout = 100000;
             request3.AllowAutoRedirect = true;
             HttpWebResponse response3 = (HttpWebResponse)request3.GetResponse();
             StreamReader sr3 = new StreamReader(response3.GetResponseStream(), Encoding.ASCII);
             string report3 = sr3.ReadToEnd();
             return report3;
          }
      }
  }
  </code></pre>
  
  <p>But unfortunately in responce I get only HTML source code of login page and it seems that cookies don't contain session ID. All requests which I perform after that code also return HTML source of login page so I can assume that it does not login correctly.</p>