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.
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.
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)
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.
Example: Blog Controller (BlogController.cs)
Step 4. Enforcing Global Anti-Forgery Token Validation
To reduce the risk of missing the attribute on individual actions, you can enforce global anti-forgery token validation by adding a filter in Startup.cs.
Example: Adding Global Anti-Forgery Token Validation (Startup.cs)
Step 5. Configuring Anti-Forgery Token Options
Customize the anti-forgery token behavior by configuring options in Startup.cs.
Example: Customizing Anti-Forgery Token Options (Startup.cs)
GitHub Project Url
https://github.com/SardarMudassarAliKhan/CSRFAttackInAspNetCoreMVC.git
Conclusion
CSRF attacks exploit the trust a website has in the user's browser to perform unwanted actions. ASP.NET Core MVC provides robust built-in tools to prevent CSRF attacks using anti-forgery tokens. By including these tokens in your forms and validating them in your controllers, you can significantly mitigate the risk of CSRF attacks. Implementing these practices ensures your blog site maintains high-security standards, protecting both your application and your users.