Jon West
Jon West

Reputation: 1

C# Login To xenForo Forum Using Webrequests?

I'm trying to login to a xenForo forum using a webrequest in C# but I just can't seem to get it to work properly so any help would be highly appreciated.

I used Fiddler to get the POST data that was sent when I logged in and this is the raw POST data I got...

POST http://www.----------.com/login/login HTTP/1.1
Host: www.----------.com
Connection: keep-alive
Content-Length: 109
Cache-Control: max-age=0
Origin: http://www.----------.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11
Content-Type: application/x-www-form-urlencoded
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Referer: http://www.----------.com/login/login
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: xf_session=0c4e132b44ce81bdf93e70c57fe17eb6; __cfduid=d43498195638b2afe52ebaa9e1f97b8b31342586809; __cfduid=d43498195638b2afe52ebaa9e1f97b8b31342586809

login=USERNAME&register=0&password=PASSWORD&remember=1&cookie_check=1&redirect=forum%2F&_xfToken=

So after that I went to create a webrequest in an attempt to replicate this and this is what I currently have...

private void button1_Click(object sender, EventArgs e)
        {
        try
        {
        HttpWebRequest http = WebRequest.Create("http://www.----------.com/login/login") as HttpWebRequest;
        http.KeepAlive = true;
        http.Method = "POST";
        http.AllowAutoRedirect = true;
        http.ContentType = "application/x-www-form-urlencoded";
        string postData="login=" + usernameBox.Text + "&register=0&password=" + passwordBox.Text + "&remember=1&cookie_check=1&redirect=forum%2F&_xfToken=";
        byte[] dataBytes = UTF8Encoding.UTF8.GetBytes(postData);
        http.ContentLength = dataBytes.Length;
        using (Stream postStream = http.GetRequestStream())
        {
        postStream.Write(dataBytes, 0, dataBytes.Length);
        }
        HttpWebResponse httpResponse = http.GetResponse() as HttpWebResponse;
        int y = (int)httpResponse.StatusCode;
        MessageBox.Show(Convert.ToString(y), "Response Code Debug");
        foreach(Cookie c in httpResponse.Cookies)
         {
         MessageBox.Show(c.Name + " = " + c.Value, "Cookie Debug");
         }
        http = WebRequest.Create("http://www.----------.com/forum") as HttpWebRequest;
        http.CookieContainer = new CookieContainer();
        http.CookieContainer.Add(httpResponse.Cookies);
        http.AllowAutoRedirect=false;
        HttpWebResponse httpResponse2 = http.GetResponse() as HttpWebResponse;
        }
        catch (Exception ex)
        {
        MessageBox.Show(ex.Message, "Catch Debug");
        try
         {
         Clipboard.SetText(ex.Message);
         }
        catch
         {
         }
        }

Just ignore some of that, I was using messageboxes to try and figure out what was going on with the request more precisely but it didn't really help much unfortunately. This is my first time working with webrequests so I apologize if it's just some sort of silly mistake.

I just need to be able to find out whether or not the login was successful. Also if you need a xenForo site to work with just use http://www.shadygamer.com which is the site I'm trying to work with.

As I said before, any and all help is appreciated. Thank you. :)

Upvotes: 0

Views: 2151

Answers (1)

carck3r
carck3r

Reputation: 317

You have to send this:

cookie_check=0

not this:

cookie_check=1

Xenforo CMS will not check cookies. It should help :).

Upvotes: 1

Related Questions