本文介绍了与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
发布评论