liveBook · Manning

Cross-site request forgery

As explained in ASP.NET Core in Action, 3e

Cross-site request forgery (CSRF) is an attack in which a victim's authenticated browser is induced to submit an unintended request to a trusted application.

Cross-site request forgery (CSRF) lets a malicious website submit a request to another site as though it came from a logged-in user. It matters especially when authentication uses cookies: the browser sends the destination site’s cookies with a form POST, even when the form came from an unrelated site. section 29.2

A forged bank transfer

A user logs in to a banking site, then visits an attacker-controlled site. That site submits a form matching the bank’s transfer form. The browser includes the bank’s authentication cookie, so requiring a login alone does not reveal that the user never intended the transfer. section 29.2

Antiforgery tokens provide an additional check. A legitimate form contains a hidden token, while a related token is stored in a cookie. The browser can send the cookie during a forged POST, but the malicious site cannot reproduce the form’s hidden token. Validation rejects the request when the tokens do not match. section 29.5

Figure 29.5 Protecting against a CSRF attack using antiforgery tokens. The browser automatically forwards the cookie token, but the malicious site can’t read it and so can’t include a token in the form. The app rejects the malicious request because the tokens don’t match.
CH29_F05_Lock3

Compare the legitimate form’s two token parts with the forged request, which has only the automatically forwarded cookie token. section 29.2

In Razor Pages, keep the built-in protection unless you have a reason to change it:

  • Forms created with Razor receive a hidden __RequestVerificationToken field, and inbound tokens are validated automatically. section 29.5 section 18.2.1
  • The Form Tag Helper can omit its token when asp-antiforgery="false" is set; [IgnoreAntiForgeryToken] can disable validation. Understand the implications before using either option. section 18.2.1 section 29.5

How ASP.NET Core in Action, 3e covers it

How deep it goes
Works through a forged banking POST and token-pair validation, including automatic framework behavior and opt-outs.
Examples
Uses a forged bank transfer and a rendered form’s __RequestVerificationToken hidden input.
What it assumes
Starts with a logged-in user and a POST form, then explains automatic cookie sending before introducing antiforgery tokens.
What you can do after
Apply it via Razor Pages’ automatic antiforgery tokens and validation while avoiding unnecessary opt-outs.
How it connects
Links cookie-based authentication, POST forms, antiforgery tokens, Razor Pages, and the Form Tag Helper.

Also explained in

Compare how 5 books explain cross-site request forgery →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.