Automatically Validate Antiforgery Tokens in ASP.NET Core MVC

Protect your POST endpoints from Cross-Site Resource Forgery (CSRF)

ebeeraheem

View Profile
132 views
Jul 08, 2025

Manually add the [ValidateAntiForgeryToken] attribute to all POST endpoints to protect against CSRF

Or you can simply

builder.Services.AddControllersWithViews(options =>
{
   options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute()); // CSRF protection
});

Done. No need to remember to decorate all your POST action methods with the attribute.