令牌验证方法如何防止 asp.net mvc3 中的 CSRF
我读到了如何使用 <@Html.AntiForgeryToken()>在隐藏字段中生成加密值,该值还将与作为会话 cookie 存储在用户浏览器中的另一个值相匹配。
但我的问题是:- 1.会话cookie中的值也会被加密吗? 2.如果是,那么操作控制器上的 [ValidateAntiforgeryToken] 如何知道如何解密这两个值并匹配它们?
BR
i read about how to use the <@Html.AntiForgeryToken()> to generate an encrypted value in a hidden field, which will also match another value that is stored as a session cookie in the user’s browser.
But my questions are:-
1. will the value in the session cookie be encrypted also,
2. and if yes then how the [ValidateAntiforgeryToken] on the action controller will know how to decrypt both values and match them?
BR
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
?它代表一个令牌。它与用于隐藏字段的值相同。实际上,
Html.AntiForgeryToken()
帮助器做了两件事。它生成令牌并将其呈现在隐藏字段中,并且还设置具有相同值的 cookie。它使用与经典 WebForms 用于加密/解密 ViewState 相同的加密/解密算法。它是一种基于机器密钥的对称加密算法。这就是为什么如果您在网络场中运行,则应确保在所有节点上拥有相同的计算机密钥,因为如果在网络场的一个节点上生成并加密防伪令牌,则可能无法在另一节点上解密如果机器密钥不匹配,则发送 POST 请求时的节点。
Yes. It represents a token. And it's the same value as the one used for the hidden field. Actually it's the
Html.AntiForgeryToken()
helper that does 2 things. It generates the token and puts renders it in a hidden field and it also sets a cookie with the same value.It uses the same encryption/decryption algorithm that classic WebForms use to encrypt/decrypt ViewState. It's a symmetric encryption algorithm based on the machine keys. That's why if you are running in a web farm you should ensure that you have the same machine keys across all nodes because if an anti forgery token was generated and encrypted on one node of the web farm it might not be able to be decrypted on another node when the POST request is sent if the machine keys do not match.