简体   繁体   English

如何在第一个请求中传递 csrf-token

[英]how to pass csrf-token in first request

my question is how cookies works, this question is arrived in my mind when i loaded my page for first time i got this我的问题是 cookie 是如何工作的,当我第一次加载我的页面时,我想到了这个问题

REQUEST HEADER
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
Accept-Encoding: gzip, deflate, br
Accept-Language: en-GB,en;q=0.9,en-US;q=0.8
Cache-Control: max-age=0
Connection: keep-alive
Cookie: csrftoken=gsZxmbW4XUpE6YnaQhlrAx9JduyExVgzWEo4fXhcY4V3fbHWVtwf0msbDQDT5r43
Host: 127.0.0.1:8000
Upgrade-Insecure-Requests: 1

when first request was sent it already had csrftoken in cookie发送第一个请求时,cookie 中已经包含 csrftoken

i tried same in incognito window than also i got same result.我在隐身窗口中尝试了相同的结果,但也得到了相同的结果。 how can my browser already have cookie without any communication to server我的浏览器如何在没有与服务器进行任何通信的情况下已经有 cookie

i am working on django with angular 7, problems is that i am sending my request from angular我正在使用 angular 7 开发 django,问题是我正在从 angular 发送我的请求

 this.http.post('http://127.0.0.1:8000/',data,  {observe : "response", withCredentials: true } )}

but in response of that i am not getting any csrftoken in setcookie .但作为回应,我在 setcookie 中没有得到任何 csrftoken。

please help me ..... sorry for adding two problems in one question but both are indirectly connected to each other请帮帮我..... 抱歉在一个问题中添加了两个问题,但两者都间接相互关联

actually thing was that django was running on 127.0.0.1 and angular was running on localhost( any loop back address) and that's why for security(CORS) issue, browser(chrome) was not allowing me to neither send cookie in request nor set cookie in response .实际上,django 在 127.0.0.1 上运行,而 angular 在 localhost(任何环回地址)上运行,这就是为什么出于安全(CORS)问题,浏览器(chrome)不允许我在请求中发送 cookie 或设置 cookie作为回应。 so i had two options one change same-site to none in browser or change my angular setting to render on 127.0.0.1.所以我有两个选择,一个是在浏览器中将相同站点更改为无,或者更改我的角度设置以在 127.0.0.1 上呈现。

CSRF 只能停止到 PUT、POST 和 DELETE,它总是以 GET 请求打开,如果您能够在第一次通过 GET 发送它,那就太好了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM