【发布时间】:2018-10-23 02:57:27
【问题描述】:
我从 Django 收到一条错误消息,指出“CSRF 令牌丢失或不正确”及其标准错误消息。当您使用标准浏览器和 Django 服务器时,许多其他问题已经涵盖了正确的响应,但我试图让 UnityWebRequest 与 Django 很好地配合。
从概念上讲,我有两个步骤。首先,我在登录页面上调用 GET 请求,解析表单中的 csrfmiddlewaretoken 和 set-cookie 标头中的 csrftoken。
然后,我创建了第二个 UnityWebRequest,其中包含“referer”作为标头,csrftoken 作为 cookie 中的标头。我会在表单中添加用户名和密码以及csrfmiddlewaretoken,但无论哪种方式都会出现错误。
是否需要设置其他标头或 cookie?我认为我的问题是我试图在 Unity 中模拟 Web 浏览器的行为,这很复杂。
UnityWebRequest loginPage = UnityWebRequest.Get("https://dividedsky.herokuapp.com/accounts/login/");
yield return loginPage.SendWebRequest ();
if(loginPage.isNetworkError || loginPage.isHttpError) {
Debug.Log(loginPage.error);
yield break;
}
// get the csrf cookie
string SetCookie = loginPage.GetResponseHeader ("set-cookie");
Debug.Log (SetCookie);
Regex rxCookie = new Regex("csrftoken=(?<csrf_token>.{64});");
MatchCollection cookieMatches = rxCookie.Matches (SetCookie);
string csrfCookie = cookieMatches[0].Groups ["csrf_token"].Value;
// get the middleware value
string loginPageHtml = loginPage.downloadHandler.text;
Regex rxMiddleware = new Regex("name='csrfmiddlewaretoken' value='(?<csrf_token>.{64})'");
MatchCollection middlewareMatches = rxMiddleware.Matches(loginPageHtml);
string csrfMiddlewareToken = middlewareMatches[0].Groups ["csrf_token"].Value;
/*
* Make a login request.
*/
List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
//formData.Add( new MultipartFormDataSection("username=fake") );
//formData.Add (new MultipartFormDataSection ("password=notpass"));
//formData.Add(new MultipartFormDataSection("csrfmiddlewaretoken=" + csrfMiddlewareToken));
UnityWebRequest doLogin = UnityWebRequest.Post("https://dividedsky.herokuapp.com/accounts/login/", formData);
doLogin.SetRequestHeader ("referer", "https://dividedsky.herokuapp.com/accounts/login/");
Debug.Log (doLogin.GetRequestHeader ("cookie"));
doLogin.SetRequestHeader ("cookie", "csrftoken=" + csrfCookie);
Debug.Log (doLogin.GetRequestHeader ("cookie"));
yield return doLogin.SendWebRequest ();
Debug.Log (doLogin.downloadHandler.text);
【问题讨论】:
标签: django unity3d csrf django-csrf