python - How to authenticate in django app via C#? -
i have 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/ - login url in django app. script prints 1 url if authentication succeed , in other case. tried rewrite 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 same result in c# code in python?
i setted django rest api url simple post request destination , django returned token in data.
Comments
Post a Comment