admin管理员组文章数量:1432446
I want my websites login form to have a new CSRF token generated everytime the page is refreshed.
I tried calling
logout(request)
request.session.flush()
But the hidden form field always has the same token, even after server restart.
This obviously means that django is reading the data from the cookie. How do I make it such that it ignores the cookies and generates a fresh one?
Alternatively is there a way for me to have an intermediate page that clears all cookies before going to the actual login page? How does one delete all cookies for my domain in Javascript?
I want my websites login form to have a new CSRF token generated everytime the page is refreshed.
I tried calling
logout(request)
request.session.flush()
But the hidden form field always has the same token, even after server restart.
This obviously means that django is reading the data from the cookie. How do I make it such that it ignores the cookies and generates a fresh one?
Alternatively is there a way for me to have an intermediate page that clears all cookies before going to the actual login page? How does one delete all cookies for my domain in Javascript?
Share Improve this question asked Mar 16, 2015 at 16:25 rep_movsdrep_movsd 6,9054 gold badges35 silver badges38 bronze badges2 Answers
Reset to default 6You can manually reset the token as follows:
from django.middleware.csrf import _get_new_csrf_key
request.META["CSRF_COOKIE_USED"] = True
request.META["CSRF_COOKIE"] = _get_new_csrf_key()
In Django >= 1.6, you should instead use django.middleware.csrf.rotate_token(request)
, which does exactly this.
Try to set the CSRF_COOKIE_AGE=None
to use session-based CSRF cookies, which keep the cookies in-memory instead of on persistent storage. I guess the CSRF token should then change if you logout a user. See the docs.
本文标签: javascriptGenerate fresh django CSRF token on each loginStack Overflow
版权声明:本文标题:javascript - Generate fresh django CSRF token on each login - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745602083a2665625.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论