1

I have the following code:

HttpWebRequest tokenRequest = (HttpWebRequest)WebRequest.Create("http://carkit.kg");
tokenRequest.CookieContainer = new CookieContainer();
string token = "";
using (var response = (HttpWebResponse)tokenRequest.GetResponse()) {
    token = response.Cookies["csrftoken"].ToString().Split('=')[1];
}

HttpWebRequest loginRequest = (HttpWebRequest)WebRequest.Create("http://carkit.kg");

var cache = new CredentialCache();
cache.Add(new Uri("http://carkit.kg/"), "Basic", new NetworkCredential(tempEmail, tempPass));
loginRequest.Credentials = cache;
loginRequest.PreAuthenticate = true;

loginRequest.Method = "POST";
loginRequest.CookieContainer = new CookieContainer();
loginRequest.CookieContainer.Add(new Cookie("csrftoken", token) {Domain="carkit.kg"});
Debug.Log(token);

byte[] data = Encoding.UTF8.GetBytes("username=" + tempEmail + "&password=" + tempPass + "&csrfmiddlewaretoken=" + token);

//loginRequest.ContentType = "application/x-www-form-urlencoded";
loginRequest.ContentLength = data.Length;
loginRequest.Timeout = 3000;
loginRequest.GetRequestStream().Write(data, 0, data.Length);
Debug.LogWarning(loginRequest.Headers.ToString());
HttpWebResponse authResponse = (HttpWebResponse)loginRequest.GetResponse();
Debug.Log(authResponse.ResponseUri);

authResponse.ResponseUri should be http://carkit.kg if password is incorrect and carkit.kg/game in other case.

last request have same url as first, but I'm getting 403 error on second one.

There is a code on python that makes work that I want C# code able to do:

import urllib2

main_page_request = requests.get('http://carkit.kg/')
csrf_cookie = main_page_request.cookies.get('csrftoken', '')

r = requests.post('http://carkit.kg/', data={u'username': u'admin', u'password': u'admin', 'csrfmiddlewaretoken': csrf_cookie }, cookies={'csrftoken': csrf_cookie})
print r.url
Vassily
  • 5,263
  • 4
  • 33
  • 63

1 Answers1

2

My guess is you are not entering the credential information in the right format. I would use a CredentialCache and set PreAuthenticate=true Here is code for that:

var cache = new CredentialCache();
cache.Add(new Uri(uri), "Digest", new NetworkCredential(username, password));
httpRequest.Credentials = cache;
httpRequest.PreAuthenticate = true;

For fixing 411 error try this: Why I get 411 Length required error?

Why are you adding 1 to data.length? I would try

loginRequest.ContentLength = data.Length;

Community
  • 1
  • 1
Seth Kitchen
  • 1,526
  • 19
  • 53
  • I tried your code (Question Updated). But it doesn't helped(( Request is timed out – Vassily Oct 01 '15 at 09:07
  • Does your webserver have basic or digest authentication? It sounds like a problem with your webserver not your code – Seth Kitchen Oct 01 '15 at 15:33
  • 1
    yep, I solved the problem: I've setted ContentLength as data.Length+1. I remove " + 1" and it starts working. Thank you. But now I'm getting 403 error on the same place with code above (updated) – Vassily Oct 01 '15 at 15:42
  • Yep that's an authentication issue which I have already explained. Check how your webserver does authentication-Basic or Digest- make the changes above – Seth Kitchen Oct 01 '15 at 15:46
  • Also I tried Negotiate and Bearer. And I tried to add domain to NetworkCredentials constructor. Nothing helped – Vassily Oct 01 '15 at 16:15
  • must be the webserver then – Seth Kitchen Oct 01 '15 at 17:23