剃刀和防伪

Razor and Antiforgery

我最近在 Microsoft 文档 (https://docs.microsoft.com/en-us/aspnet/core/security/anti-request-forgery) 上看到了以下注释:

Razor Pages are automatically protected from XSRF/CSRF. You don't have to write any additional code. See XSRF/CSRF and Razor Pages for more information.

指向另一个页面 (https://docs.microsoft.com/en-us/aspnet/core/mvc/razor-pages/index?tabs=visual-studio#xsrf),其中显示:

You don't have to write any code for antiforgery validation. Antiforgery token generation and validation are automatically included in Razor Pages.

我将 Razor 与我的 ASP.NET MVC 应用程序一起使用,还使用 ​​AntiForgeryToken 助手保护我的表单。由于防伪令牌相互验证的方式(隐藏字段 + cookie),我的用户 必须 允许在网站上使用 cookie。

我现在对我在文档中读到的内容感到困惑,因为它似乎说我在使用 Razor 时不需要使用 @Html.AntiForgeryToken() 帮助程序或 [ValidateAntiForgeryToken] 属性... ?

作为一个附加问题,有没有一种方法可以在不使用 cookie 的情况下保护我的网站免受 CSRF 攻击?

阅读 post (https://docs.microsoft.com/en-us/aspnet/core/security/anti-request-forgery) 中的第一个 link 我找到了解释:

ASP.NET Core implements anti-request-forgery using the ASP.NET Core data protection stack.

In ASP.NET Core MVC 2.0 the FormTagHelper injects anti-forgery tokens for HTML form elements.

对于旧版本的 ASP.NET MVC,需要您提到的助手。