Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过C#在django应用程序中进行身份验证?_C#_Python_Django - Fatal编程技术网

如何通过C#在django应用程序中进行身份验证?

如何通过C#在django应用程序中进行身份验证?,c#,python,django,C#,Python,Django,我有一个python脚本: 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', 'csrfmiddlew

我有一个python脚本:

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/-是django应用程序中的登录url。如果验证成功,脚本将打印一个url,在其他情况下打印另一个url。我试图用C#(Unity3D游戏)重写这个脚本:

但我收到错误411或请求超时。如何在C代码中获得与python相同的结果?

我将其设置为简单的post请求目的地,django在数据中返回令牌

//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();