C#, Login to Wordpress

tehmadcracker

BANNED
Joined
Apr 19, 2009
Messages
155
Reaction score
509
I am having a hell of a time trying to get my c# code to login to the wordpress admin dashboard, has anyone on here dealt with this before? I sniffed the packets from FF->Dashboard, and from my app to the dashboard, I have all my cookies etc, but I must be missing something, any ideas are greatly appreciated
 
What code are you using?

Are you using the correct header? I.e. GET

Wordpress implements several redirects during login.
 
public class WPWorkerTests
{
public CookieContainer cookieJar = new CookieContainer();

public WPWorkerTests()
{

}

public void StartAdmin(object obj)
{
CookieContainer cJar = new CookieContainer();
string primer = getRequest("url to wordprss login", 0);
string respAdmin = postRequest(wpaObject.url, "log=admin&pwd=adminpw&wp-submit=Log+In&testcookie=1&redirect_to=http%3A%2F%2Fwordpress domain%2Fwp-admin%2F", 0);
string ha = getRequest("url to wordpress admin", 0);

}

public string getRequest(string url, int tries)
{
try
{

HttpWebRequest HttpWRequest = (HttpWebRequest)WebRequest.Create(url);
HttpWRequest.CookieContainer = cookieJar;
HttpWRequest.UserAgent = "IE 9 Experimental";
HttpWRequest.KeepAlive = true; //this is the default

HttpWRequest.Timeout = 10000;
HttpWRequest.Method = "GET";
HttpWRequest.ServicePoint.ConnectionLimit = 100;

HttpWebResponse HttpWResponse = (HttpWebResponse)HttpWRequest.GetResponse();
StreamReader sr = new StreamReader(HttpWResponse.GetResponseStream(), Encoding.ASCII);
string response = sr.ReadToEnd().ToLower();
sr.Close();

return response.ToLower();
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + url);

if (ex.ToString().Contains("404") == true)
{
return "404";
}
else if (ex.ToString().Contains("500") == true)
{
return "";
}

if (tries > 1)
{
Console.WriteLine();
Console.WriteLine(url);
Console.WriteLine();
Console.WriteLine(ex.ToString());
}
else
{
tries++;
System.Threading.Thread.Sleep(500);
return getRequest(url, tries);
}
}
return "";
}

public string postRequest(string url, string postdata, int tries)
{
try
{
HttpWebRequest HttpWRequest = (HttpWebRequest)WebRequest.Create(url);
HttpWRequest.CookieContainer = cookieJar;
HttpWRequest.UserAgent = "IE 9 Experimental";
HttpWRequest.KeepAlive = true; //this is the default
HttpWRequest.Referer = url;

HttpWRequest.Timeout = 120000;
HttpWRequest.Method = "POST";
HttpWRequest.Referer = url;
// add the content type so we can handle form data
HttpWRequest.ContentType = "application/x-www-form-urlencoded";
// we need to store the data into a byte array
byte[] PostData = System.Text.Encoding.ASCII.GetBytes(postdata);
HttpWRequest.ContentLength = PostData.Length;
Stream tempStream = HttpWRequest.GetRequestStream();
// write the data to be posted to the Request Stream
tempStream.Write(PostData, 0, PostData.Length);
tempStream.Close();

HttpWebResponse HttpWResponse = (HttpWebResponse)HttpWRequest.GetResponse();
StreamReader sr = new StreamReader(HttpWResponse.GetResponseStream(), Encoding.ASCII);
string response = sr.ReadToEnd().ToLower();
sr.Close();

return response.ToLower();


}
catch (Exception ex)
{
if (tries > 1)
{
Console.WriteLine(ex.ToString());
}
else
{
tries++;
System.Threading.Thread.Sleep(500);
return postRequest(url, postdata, tries);
}
}
return "";
}

}
 
What I did: I extended the xmlrpc interface of wordpress and wrote in java an xmlrpc submitter. Works very well. I initially also wanted to work with the admin interface, but this is very tedious and above all when an update of wordpress happens, you have to adjust many things. So my advice: have a look at /xmlrpc.php, check which funtions are already available (many!) and extend for what you need.
 
Back
Top