AntiForgeryToken 和 ValidateAntiForgeryToken 的意外行为
我已经开始在一些表单中使用 AntiForgeryToken 来防止跨站点请求伪造。然而,我遇到了一些奇怪的行为,只是想澄清这是一个错误还是只是我做错了什么。我在表单中使用 Html.AntiForgeryToken()
调用。然后,我在表单发布到的操作方法中使用 [ValidateAntiForgeryToken]
属性。我现在没有使用盐。
我的理解是 Html.AntiForgeryToken()
生成一个名为 __RequestVerificationToken
的隐藏输入和一个名为 __RequestVerificationToken_Lw__
的 cookie,它们都应该包含相同的值。
然而,我遇到的行为是:
- 无论您使用多少次,cookie 始终具有相同的值 获取页面
- 每次获取页面时,隐藏输入都有不同的值
- ValidateAntiForgeryToken 每次都会进行验证,即使是从 CSRF 场景中的不同站点。
- 如果我更改国外站点中隐藏输入的值, 令牌不验证(预期的行为,但为什么它验证 当隐藏的输入/cookie值不同时?)
有人有什么想法吗?
I've started using AntiForgeryToken in some of my forms to prevent cross site request forgery. However I am getting some weird behaviour and just wanted to clarify whether this is a bug or just me doing something wrong. I am using the Html.AntiForgeryToken()
call in my form. I then use the [ValidateAntiForgeryToken]
attribute in the action method that the form posts to. I'm not using a salt at this point.
My understanding is that Html.AntiForgeryToken()
generates a hidden input with a name of __RequestVerificationToken
and a cookie named __RequestVerificationToken_Lw__
, which should both contain the same value.
The behaviour I am experiencing however is that:
- The cookie always has the same value no matter how many times you
GET the page - The hidden input has a different value every time you GET the page
- The ValidateAntiForgeryToken validates every time, even from a
different site in a CSRF scenario. - If I change the value of the hidden input in the foreign site, the
token doesn't validate (expected behaviour, but why does it validate
when the hidden input/cookie value is different?)
Anyone got any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于第三点,您是否在 CSRF 场景中包含隐藏字段?
AntiForgeryToken 的安全性在于隐藏输入仅存在于您的域所服务的页面中,并且不能被其他域复制或捕获。如果您模拟了一个通过隐藏输入的测试,那么这不是一个有效的测试。
我建议您阅读 Phil Haack 的这篇文章:Anatomy of a跨站请求伪造攻击
For number 3, are you including the hidden field in your CSRF scenario?
The safety of the AntiForgeryToken is that the hidden input exists only in the page served by your domain, and cannot be copied or captured by another domain. If you have mocked up a test which passes the hidden input, then that is not a valid test.
I suggest you read this article from Phil Haack: Anatomy of a Cross-site Request Forgery Attack