How to authenticate in django app via C#?
Question: I have a python script:

    
    
    import requests
    
    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
    

carkit.kg/ - is a login url in django app. Script prints one url if
authentication succeed and another in other case. I tried to rewrite this
script in C# (Unity3D game):

    
    
    //get token
    string url = "http://carkit.kg";
    HttpWebRequest tokenRequest = (HttpWebRequest)WebRequest.Create(url);
    tokenRequest.CookieContainer = new CookieContainer();
    HttpWebResponse tokenResponse = (HttpWebResponse)tokenRequest.GetResponse();
    String token = tokenResponse.Cookies["csrftoken"].ToString().Split('=')[1];
    
    //login 
    HttpWebRequest loginRequest = (HttpWebRequest)WebRequest.Create(url);
    loginRequest.Method = "POST";
    loginRequest.CookieContainer = new CookieContainer();
    loginRequest.ContentType = "application/x-www-form-urlencoded";
    
    loginRequest.CookieContainer.Add(new Cookie("csrftoken", token, "/", "carkit.kg"));
    String postData = "username=" + tempEmail;
    postData += "&password=" + tempPass;
    postData += "&csrfmiddlewaretoken=" + token;
    byte[] data = Encoding.ASCII.GetBytes(postData);
    loginRequest.ContentLength = data.Length + 1;
    Debug.Log(data.Length);
        loginRequest.Timeout = 3000;
    
        String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(tempEmail + ":" + tempPass));
        loginRequest.Headers.Add("Authorization", "Basic " + encoded);
    
    loginRequest.GetRequestStream().Write(data, 0, data.Length);
    loginRequest.PreAuthenticate=true;
    
    HttpWebResponse authResponse = (HttpWebResponse)loginRequest.GetResponse();
    

But I'm getting error 411 or timed out request. How to get the same result in
C# code as in python?


Answer: I setted [django rest api url](http://www.django-rest-framework.org/api-
guide/authentication/) as simple post request destination and django returned
token in data.

