简体   繁体   English

AntiForgeryToken 在幕后如何工作?

[英]How does AntiForgeryToken work behind the scenes?

I know the basics of CSRF and AntiForgeryToken .我知道CSRFAntiForgeryToken的基础知识。

I also know that there are many similar questions around but none seemed to describe the implementation details, which is the part I'm interested in.我也知道周围有很多类似的问题,但似乎没有一个描述实现细节,这是我感兴趣的部分。

Each time you call Html.AntiForgeryToken() , it generates a new random token :每次调用Html.AntiForgeryToken() ,它都会生成一个新的随机令牌:

  1. Can I have multiple active tokens at the same time?我可以同时拥有多个活动令牌吗? (I assume it's yes here) (我认为这里是肯定的)
  2. Can I use any of those tokens in another form?我可以以其他形式使用这些代币吗? (I assume it's yes here) (我认为这里是肯定的)
  3. Can I use the same token more than once?我可以多次使用同一个令牌吗?
  4. Is there a security reason why the token is random everytime?令牌每次都是随机的是否有安全原因? Couldn't it be the same token for the whole session?整个会话不能是同一个令牌吗?

The token is stored in a cookie令牌存储在 cookie 中

  1. When I have multiple forms and tokens in my page, does it mean I have multiple cookies or only 1 cookie containing all the active tokens?当我的页面中有多个表单和令牌时,是否意味着我有多个 cookie 或只有 1 个包含所有活动令牌的 cookie?

Also...还...

  1. How is the token generated and is it possible to validate it manually.令牌是如何生成的,是否可以手动验证。

1.Can I have multiple active tokens at the same time? 1.我可以同时拥有多个活动代币吗? (I assume it's yes here) (我认为这里是肯定的)

-> Yes. -> 是的。

2.Can I use any of those tokens in another form? 2.我可以以其他形式使用这些代币吗? (I assume it's yes here) (我认为这里是肯定的)

-> Yes. -> 是的。

3.Can I use the same token more than once? 3.我可以多次使用同一个令牌吗?

-> Short answer is yes you can. -> 简短的回答是可以。 Long answer: It depends on multiple factors, you would have to regenerate the token if,长答案:这取决于多种因素,如果出现以下情况,您将不得不重新生成令牌,

Your cookie expires
Your user identity has changed.       
Your additional data provider has decided that your token is no  
longer valid.

4.Is there a security reason why the token is random every time? 4.令牌每次都是随机的,是否有安全原因? Couldn't it be the same token for the whole session?整个会话不能是同一个令牌吗?

-> You don't want anyone to be able to predict your token. -> 您不希望任何人能够预测您的令牌。 Remember there is no inbuilt auto expiration of the cookie token.请记住,cookie 令牌没有内置的自动过期功能。

1.When I have multiple forms and tokens in my page, does it mean I have multiple cookies or only 1 cookie containing all the active tokens? 1.当我的页面中有多个表单和令牌时,是否意味着我有多个 cookie 或只有 1 个包含所有活动令牌的 cookie?

-> At any given point of time there would be only one cookie that will be active for a session, that cookie has a SecurityToken property. -> 在任何给定的时间点,只有一个 cookie 将在会话中处于活动状态,该 cookie 具有 SecurityToken 属性。 Every valid anti-forgery token on the page would have the same SecurityToken (otherwise it would fail validation).页面上的每个有效防伪令牌都将具有相同的 SecurityToken(否则将无法通过验证)。

1.How is the token generated and is it possible to validate it manually. 1.token是如何生成的,是否可以手动验证。

I would suggest to read the post explaining the internals here and code here我建议阅读解释内部结构代码的帖子

I am actually thinking why would you need to use the same anti-forgery token across forms.. it might be helpful if you share your scenario.我实际上在想为什么您需要跨表单使用相同的防伪令牌..如果您分享您的场景可能会有所帮助。

对于最后一个问题,您可以使用以下内容验证 AntiForgeryTokens:

System.Web.Helpers.AntiForgery.Validate(cookie.Value, formValue);

Answering in terms of ASP.NET Core just in case anyone stumbles upon this post from Google.就 ASP.NET Core 进行回答,以防万一有人偶然发现了来自 Google 的这篇文章。

The following is based off this article :以下内容基于本文

Can I have multiple active tokens at the same time?我可以同时拥有多个活动令牌吗?

Yes.是的。 If you have two tabs open and hit the same URL that returns to you a page with a form containing an antiforgery token, you'll have a different token in the HTML on each page.如果您打开两个选项卡并点击同一个 URL 返回一个页面,该页面包含一个包含防伪标记的表单,那么每个页面的 HTML 中都会有一个不同的标记。 But you'll have the same token in your cookie.但是您的 cookie 中将具有相同的标记。

Can I use any of those tokens in another form?我可以以其他形式使用这些代币吗?

Yes.是的。 You can test this by hitting the URL in two tabs, open Developer Options and swap out the tokens, and the application won't know any better.您可以通过点击两个选项卡中的 URL 来测试这一点,打开开发人员选项并换出令牌,应用程序不会有任何更好的了解。

Can I use the same token more than once?我可以多次使用同一个令牌吗?

Yes.是的。 As long as the token in the HTML was generated along with your cookie, then if you're using the same cookie, you could use the same token in your HTML as before.只要 HTML 中的标记与您的 cookie 一起生成,那么如果您使用相同的 cookie,您就可以像以前一样在 HTML 中使用相同的标记。

Is there a security reason why the token is random everytime?令牌每次都是随机的是否有安全原因? Couldn't it be the same token for the whole session?整个会话不能是同一个令牌吗?

See the accepted answer by Harsh Gupta.请参阅 Harsh Gupta 接受的答案。

When I have multiple forms and tokens in my page, does it mean I have multiple cookies or only 1 cookie containing all the active tokens?当我的页面中有多个表单和令牌时,是否意味着我有多个 cookie 或只有 1 个包含所有活动令牌的 cookie?

See the accepted answer by Harsh Gupta.请参阅 Harsh Gupta 接受的答案。

How is the token generated and is it possible to validate it manually.令牌是如何生成的,是否可以手动验证。

Here is a summary from the article I linked above:这是我上面链接的文章的摘要:

  • Build a byte array using a random number generator.使用随机数生成器构建字节数组。

  • Generate a unique array based on step 1.根据步骤 1 生成唯一数组。

  • Encrypt the array.加密数组。

  • Prepend a magic header and the defaultKeyId to the encrypted array.在加密数组中添加一个魔法头和 defaultKeyId。

  • Base64URL encode the encrypted array and use this as the token string. Base64URL 对加密数组进行编码并将其用作令牌字符串。

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

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