Introduction
Cross-site request Forgery (CSRF) is a type of attack where unauthorized commands are transmitted from a user that a web application trusts. This type of attack can force a user's browser to send unwanted requests to a web application on which the user is authenticated, potentially leading to unintended actions on the user's behalf.
How do CSRF Attacks work?
A typical CSRF attack involves the following steps,
- User Authentication: The user logs into a trusted website, which sets a session cookie in the browser.
- Malicious Site: The attacker tricks the user into visiting a malicious site.
- Crafted Request: The malicious site contains a crafted request that targets the trusted website.
- Request Execution: The browser, still authenticated with the trusted website, sends the forged request, which is then processed by the server as a legitimate request.
Real-World Example A Blog Site
Imagine a blog site where users can create posts by making a POST request to /Blog/Create with parameters like title, content, and csrf_token. Without proper CSRF protection, an attacker can create a form like this.
If you are not including the asp-antiforgery="true" then an attacker can submit the malicious data to your website.
@model XSSAttackInAspNetCoreMVC.Model.BlogPost
<!DOCTYPE html>
<html>
<head>
<title>Create Blog Post</title>
</head>
<body>
<h1>Create Blog Post</h1>
<form asp-action="Create" method="post">
<div class="form-group">
<label asp-for="Title"></label>
<input asp-for="Title" class="form-control" />
<span asp-validation-for="Title" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Content"></label>
<textarea asp-for="Content" class="form-control"></textarea>
<span asp-validation-for="Content" class="text-danger"></span>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<a asp-controller="Blog" asp-action="Index">Back to List</a>
</body>
</html>
When the victim, while authenticated, visits the malicious site, this form could be submitted automatically, causing an unauthorized blog post to be created.
Mitigating CSRF in ASP.NET Core MVC
ASP.NET Core MVC provides built-in mechanisms to prevent CSRF attacks using anti-forgery tokens. Here's a step-by-step guide to implementing CSRF protection in an ASP.NET Core MVC blog site.
Step 1. Setting Up the ASP.NET Core MVC Application
First, create a new ASP.NET Core MVC application using the.NET CLI or Visual Studio.
dotnet new mvc -n BlogCSRFExample
cd CSRFAttackInAspNetCoreMVC
Step 2. Adding Anti-Forgery Tokens to Forms
In your MVC views, include an anti-forgery token in your forms using the @Html.AntiForgeryToken() helper. This adds a hidden field with a unique token for the session.
Example: Create Blog Post View (Create.cshtml)
@model XSSAttackInAspNetCoreMVC.Model.BlogPost
<!DOCTYPE html>
<html>
<head>
<title>Create Blog Post</title>
</head>
<body>
<h1>Create Blog Post</h1>
<form asp-action="Create" method="post" asp-antiforgery="true">
@Html.AntiForgeryToken()
<div class="form-group">
<label asp-for="Title"></label>
<input asp-for="Title" class="form-control" />
<span asp-validation-for="Title" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Content"></label>
<textarea asp-for="Content" class="form-control"></textarea>
<span asp-validation-for="Content" class="text-danger"></span>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<a asp-controller="Blog" asp-action="Index">Back to List</a>
</body>
</html>
Step 3. Validating the Anti-Forgery Token in the Controller
In your controller, ensure that the anti-forgery token is validated by decorating your actions with the [ValidateAntiForgeryToken] attribute.
Join the conversation! Your thoughts help the community grow.