Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Products
  • Resources
    • Cheatsheets
    • Tech Comparisons
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# ASP.NET MVC ASP.NET Web Forms C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet General Web Development HTML, CSS HTML/CSS Java JavaScript JavaScript, HTML, CSS JavaScript, Node.js Node.js
      Python Python 3.11, Pandas, SQL Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. ASP.NET Core
  4. Implementing CSRF Protection in ASP.NET Core MVC with AntiForgeryToken

Implementing CSRF Protection in ASP.NET Core MVC with AntiForgeryToken

Date- May 29,2026 270
csrf antiforgery

Overview

Cross-Site Request Forgery (CSRF) is a type of attack that tricks the victim into submitting a malicious request. This occurs when an attacker causes an end user to perform actions without their consent, often leveraging their authenticated session. CSRF attacks exploit the trust that a web application has in the user's browser. For instance, if a user is logged in to a banking website, an attacker could craft a request that transfers money from the user's account to theirs without the user's knowledge.

The primary solution to CSRF vulnerabilities is the implementation of anti-CSRF tokens. These tokens are unique and unpredictable values generated by the server and sent to the client. When the client submits a request, it must include this token, allowing the server to verify that the request is legitimate. In ASP.NET Core MVC, the AntiForgeryToken attribute is used to facilitate this protection seamlessly.

Real-world use cases include securing user profile updates, online transactions, and any form submission that performs a state-altering operation. Implementing CSRF protection is not just a best practice but a necessity for modern web applications, ensuring that user actions are intentional and authorized.

Prerequisites

  • ASP.NET Core MVC Knowledge: Familiarity with the MVC architecture and how controllers and views interact.
  • Basic Security Concepts: Understanding of web security principles, particularly session management and authentication.
  • Development Environment: Visual Studio or Visual Studio Code set up for ASP.NET Core development.
  • NuGet Packages: Ensure that the necessary ASP.NET Core packages are installed, particularly Microsoft.AspNetCore.Mvc.

Understanding AntiForgeryToken

The AntiForgeryToken is a mechanism provided by ASP.NET Core to prevent CSRF attacks. It works by generating a token that is unique to each user session and is tied to the user's identity. This token is included in forms and AJAX requests, providing a way for the server to validate the authenticity of incoming requests.

When a user accesses a page that contains a form, the server generates an AntiForgeryToken and embeds it in the HTML. Upon form submission, the token is sent back to the server, where it is validated. If the token is missing or invalid, the server rejects the request, thus mitigating the risk of CSRF attacks.

@using Microsoft.AspNetCore.Antiforgery
@inject IAntiforgery Antiforgery

In this code snippet, we import the necessary AntiForgery namespace and inject the IAntiforgery service. The form includes a hidden input field where the AntiForgeryToken is placed using GetTokens method. This ensures that when the form is submitted, the token is sent along with the other form data.

How AntiForgeryToken Works

When the user submits the form, the server checks the token against the one stored in the user's session. If they match, the request proceeds; if not, an exception is thrown. This mechanism is crucial because it ties the token to the user's session, making it difficult for attackers to forge a valid request.

Implementing AntiForgeryToken in ASP.NET Core MVC

To implement CSRF protection using AntiForgeryToken in an ASP.NET Core MVC application, you typically need to follow a few steps: configure services, decorate your controllers, and ensure your views render the token correctly.

First, ensure that your Startup class is configured to use MVC and that you add the AntiForgery service in the ConfigureServices method.

public void ConfigureServices(IServiceCollection services) {  services.AddControllersWithViews();}

The above code snippet registers the MVC services in the application's dependency injection container, which is essential for enabling the AntiForgery services.

Decorating Controllers with AntiForgery

Next, you need to decorate your controller actions with the [ValidateAntiForgeryToken] attribute. This attribute tells the framework to check for the AntiForgeryToken in incoming POST requests.

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Submit(string data) { // Process the data return RedirectToAction("Index");}

Here, the Submit action method is decorated with the [ValidateAntiForgeryToken] attribute. This ensures that the AntiForgeryToken is validated before the action executes, providing an additional layer of security.

Edge Cases & Gotchas

While implementing AntiForgeryToken, several edge cases and common pitfalls can arise. One common mistake is not including the AntiForgeryToken in AJAX requests, which can lead to failed requests.

$.ajax({  type: "POST",  url: "/Home/Submit",  data: { data: "example" },  headers: { 'RequestVerificationToken': $('input[name="__RequestVerificationToken"]').val() }});

The above AJAX request includes the AntiForgeryToken in the headers. If this header is omitted, the server will reject the request, resulting in a 403 Forbidden error.

Missing AntiForgeryToken

Another pitfall is not rendering the AntiForgeryToken in forms, leading to invalid submission. Always ensure that the token is included in every form that modifies state.

Performance & Best Practices

CSRF protections are essential, but they can introduce performance overhead if not implemented correctly. One best practice is to use ASP.NET Core's built-in mechanisms instead of custom solutions, which can be error-prone and less secure.

Additionally, consider using caching for frequently accessed pages that do not require authentication. This can help alleviate some performance hits while maintaining security.

Testing AntiForgeryToken

Testing your AntiForgeryToken implementation is crucial. Utilize unit tests to ensure that your controllers reject requests without valid tokens. You can also use integration tests to simulate user behavior and validate that the CSRF protection is functioning as expected.

[Fact]
public async Task Submit_Post_MissingToken_ReturnsForbidden() { var response = await _client.PostAsync("/Home/Submit", new StringContent("data=example")); Assert.Equal(HttpStatusCode.Forbidden, response.StatusCode);}

This test ensures that if no AntiForgeryToken is included in the request, the server responds with a 403 Forbidden status.

Real-World Scenario: Building a Secure Form

Let’s create a simple ASP.NET Core MVC application that demonstrates the use of AntiForgeryToken in a secure form for submitting user feedback.

public class FeedbackModel {  public string Message { get; set; }}

[HttpGet]
public IActionResult Feedback() { return View(); }

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Feedback(FeedbackModel model) { if (ModelState.IsValid) { // Save feedback to database return RedirectToAction("ThankYou"); } return View(model); }

In this mini-project, we define a FeedbackModel class to hold the user feedback. The Feedback action method renders the view, while the POST version validates the AntiForgeryToken. If valid, it processes the feedback; otherwise, it returns the view with validation errors.

Conclusion

  • Understanding and implementing CSRF protection is crucial for web application security.
  • The AntiForgeryToken mechanism in ASP.NET Core MVC provides a robust way to mitigate CSRF vulnerabilities.
  • Always include the AntiForgeryToken in forms and AJAX requests that modify server state.
  • Test your implementation thoroughly to ensure that it operates as intended.
  • Follow best practices to maintain performance while securing your applications.

S
Shubham Saini
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

CWE-918: Preventing Server-Side Request Forgery (SSRF) in ASP.NET Core HttpClient
May 31, 2026
Integrating LinkedIn OAuth in ASP.NET Core for Professional Login
May 01, 2026
CWE-863: Fixing Broken Access Control in ASP.NET Core MVC Controllers
Apr 23, 2026
Securing ASP.NET Core appsettings.json Using Environment Variables and Secret Management
Jun 11, 2026
Previous in ASP.NET Core
CWE-89: Preventing SQL Injection in ASP.NET Core with Dapper and …
Next in ASP.NET Core
CWE-434: Implementing Secure File Uploads in ASP.NET Core with Va…
Buy me a pizza

Comments

🔥 Trending This Month

  • 1
    CWE-269: Improper Privilege Management - Implementing the … 361 views
  • 2
    Send Email With HTML Template And PDF Using ASP.Net C# 17,222 views
  • 3
    Building Custom Bedrock Add-Ons with JavaScript: A Complet… 1,938 views
  • 4
    Error-An error occurred while processing your request in .… 11,965 views
  • 5
    Elasticsearch Integration in ASP.NET Core - Full-Text Sear… 244 views
  • 6
    Fix Gemini API Error 429: Quota Exceeded on Free Tier (Sys… 831 views
  • 7
    Integrating Google reCAPTCHA v3 in ASP.NET Core for Seamle… 614 views

On this page

🎯

Interview Prep

Ace your ASP.NET Core interview with curated Q&As for all levels.

View ASP.NET Core Interview Q&As

More in ASP.NET Core

  • How to Encrypt and Decrypt Password in Asp.Net 26685 views
  • Exception Handling Asp.Net Core 21722 views
  • HTTP Error 500.31 Failed to load ASP NET Core runtime 21179 views
  • How to implement Paypal in Asp.Net Core 20129 views
  • Task Scheduler in Asp.Net core 18204 views
View all ASP.NET Core posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1780
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Products
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • ASP.NET MVC
  • ASP.NET Web Forms
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • General Web Development
  • HTML, CSS
  • HTML/CSS
  • Java
  • JavaScript
  • JavaScript, HTML, CSS
  • JavaScript, Node.js
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor