1

I got problems while trying to authenticate user using C# code on PHP forum. So far I tried:

webClient.Credentials = new NetworkCredential("Me", "MyPassword");

then:

HtmlDocument webPage = new HtmlDocument();
webPage.Load(new MemoryStream(client.DownloadData(this.websiteUri)));

I tired also adding cookie info to the WebClient header.

Moreover I tried:

using (WebClient client = new WebClient())
            {

                byte[] response = client.UploadValues(url, new NameValueCollection()
                   {
                       { "usrname", "myLogin" },
                       { "password", "p@ssw0rd" }
                   });

                string result = System.Text.Encoding.UTF8.GetString(response);
            }

How can I check results? Am I doing somethinng wrong? Any suggestions?

Thanks!

Luke
  • 150
  • 1
  • 6

1 Answers1

0

Probably this PHP forum is using some sort of form based loggin in. To log in this case you need to send a POST request to proper page. To check, which page you have to make request to, you should inspect forum's HTML code.

Below you see some log in form for Simple Machines forum inspected via Chrome Developer Tools.

Form

Address to call you can find in property action of form tag. In your POST request you would need to include fields user and password.

As you can see, form contains also some sort of anti-forgery tokens to make this request more difficult to automate. This is very common, so you will probably need to do some HTML parsing before being able to log in.

Kędrzu
  • 2,385
  • 13
  • 22
  • OK, I tried that. After following your suggestions I'm not redirected from login.php to main forum page (response URI is same) - I assume this is not proper. I should be redirected, so response URI should be i.e.: "home.php". Moreover - am I forced to use this auth method each time I check URL? – Luke Feb 09 '15 at 19:04
  • Redirection may depend on the method used by server. Check headers of response, they may contain header "Location: some-address.php". This is common way of redirecting to other page after POST request. Second of all: if you want to stay logged in, you need to preserve cookies, that shall contain your php session. Here you have some tips how to do such: http://www.codeproject.com/Articles/195443/WebClient-Class-with-Cookies http://stackoverflow.com/questions/1777221/using-cookiecontainer-with-webclient-class – Kędrzu Feb 09 '15 at 21:19