gpt4 book ai didi

asp.net - AutoValidateAntiForgeryToken 与 ValidateAntiForgeryToken

转载 作者:行者123 更新时间:2023-12-03 01:41:27 32 4
gpt4 key购买 nike

我刚才试图通过提供防伪 token 来保护 post 方法免受侧脚本攻击,但注意到,在 .Net Core 中还有另一个名为 AutoAntiForgeryToken 的属性。 XML 注释和在线搜索没有提供有关此新属性的太多信息。

任何有关新属性的帮助和描述,我们将不胜感激。

最佳答案

来自AutoValidateAntiforgeryTokenAttribute documentation :

An attribute that causes validation of antiforgery tokens for allunsafe HTTP methods. An antiforgery token is required for HTTP methodsother than GET, HEAD, OPTIONS, and TRACE. It can be applied at as aglobal filter to trigger validation of antiforgery tokens by defaultfor an application.

AutoValidateAntiforgeryTokenAttribute 允许将防伪 token 验证全局应用于所有不安全方法,例如POST、PUT、PATCH 和 DELETE。因此,您不需要将 [ValidateAntiForgeryToken] 属性添加到每个需要它的操作中。

要使用它,请将以下代码添加到 Startup 类的 ConfigureServices 方法

services.AddMvc(options =>
{
options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute());
});

如果您需要忽略防伪验证,可以将 [IgnoreAntiforgeryToken] 属性添加到操作中。

关于asp.net - AutoValidateAntiForgeryToken 与 ValidateAntiForgeryToken,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39947460/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com