与asp.net

编程入门 行业动态 更新时间:2024-10-25 03:17:20
本文介绍了与asp-core中的AntiForgery.Validate()等效的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在asp核心中是否存在与 AntiForgery.Validate(); 类似的命令以验证动作主体中的防伪令牌?

Exists, in asp-core, a command similar to AntiForgery.Validate(); to validate antiforgery token in the action body?

dotnet 4.5.1中的代码示例:

Code example in dotnet 4.5.1:

public ActionResult Index() { if (!User.Identity.IsAuthenticated) { System.Web.Helpers.AntiForgery.Validate(); } // rest of action }

推荐答案

可以使用控制器中的过滤器属性自动完成防伪令牌验证。

Antiforgery token validation can be done automatically using filter attributes in your controllers.

  • 使用 [AutoValidateAntiforgeryToken] 来验证所有不安全方法上的令牌。 (除了GET,HEAD,TRACE,OPTIONS之外的方法)。
  • 使用 [ValidateAntiforgeryToken] 始终验证令牌
  • 使用 [IgnoreAntiforgeryToken] 忽略令牌验证
  • Use [AutoValidateAntiforgeryToken] to validate the token on all "unsafe" methods. (Methods other than GET, HEAD, TRACE, OPTIONS).
  • Use [ValidateAntiforgeryToken] to always validate the token
  • Use [IgnoreAntiforgeryToken] to ignore token validation

您可以组合这些属性来获得所需的粒度。例如:

You can combine these attributes to achieve the granularity you need. For example:

//Validate all 'unsafe' actions except the ones with the ignore attribute [AutoValidateAntiforgeryToken] public class MyApi: Controller { [HttpPost] public IActionResult DoSomething(){ } [HttpPut] public IActionResult DoSomethingElse(){ } [IgnoreAntiforgeryToken] public IActionResult DoSomethingSafe(){ } } //Validate only explicit actions public class ArticlesController: Controller { public IActionResult Index(){ } [ValidateAntiforgeryToken] [HttpPost] public IActionResult Create(){ } }

我注意到文档站点,但是您可以在github 问题。

I have noticed the documentation isnt fully ready in the docs site, but you can see a draft of it in the github issue.

更多推荐

与asp.net

本文发布于:2023-11-14 02:54:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1586025.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:asp   net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!