I have ASP.NET Core application. I have been using ValidateAntiForgeryToken
attribute on all POST
action methods so far.
Now i am thinking to useValidateAntiForgeryToken
at controller level so it can take care of both POST
and GET
methods.
Below is sample controller
[ValidateAntiForgeryToken]
public class SearchController : Controller
{
public SearchController()
{
}
[HttpGet]
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Save(MyModel model)
{
}
}
When user accesses the URL http://localhost/search, im not sure how Index
action method will receive forgerytoken? Right now i get error Bad Request
because there is no token included in the request.
From http://blog.stevensanderson.com/2008/09/01/prevent-cross-site-request-forgery-csrf-using-aspnet-mvcs-antiforgerytoken-helper/
Limitations of the Anti-Forgery helpers
So it isn't useful at the controller level.ASP.NET Core
[ValidateAntiforgeryToken]
on the controller has limitations.https://learn.microsoft.com/en-us/aspnet/core/security/anti-request-forgery?view=aspnetcore-2.1
Controller-level support is improved with
[AutoValidateAntiforgeryToken]