Cross-Site Request Forgery (CSRF) works because browsers automatically attach cookies to requests. If a user is logged in to your bank site and visits a malicious page, that page can make the browser send a request such as "transfer money" to your site, and the cookie goes along with it.
Protections:
Anti-forgery tokens: the server puts a secret token in each form and rejects a POST without a valid one. ASP.NET Core Razor Pages do this automatically, and MVC offers [ValidateAntiForgeryToken] or AutoValidateAntiforgeryToken.
SameSite cookies (Lax or Strict) stop cookies being sent on most cross-site requests. They are a good extra layer.
Never change data with GET requests. Use POST, PUT or DELETE.
For APIs that use bearer tokens in the Authorization header (not cookies), CSRF is much less of a risk, because the browser does not attach those automatically.
So yes, a cookie-based app that changes data needs protection. Check that every state-changing endpoint is covered.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Cross-Site Request Forgery (CSRF) works because browsers automatically attach cookies to requests. If a user is logged in to your bank site and visits a malicious page, that page can make the browser send a request such as "transfer money" to your site, and the cookie goes along with it.
Protections:
[ValidateAntiForgeryToken]orAutoValidateAntiforgeryToken.So yes, a cookie-based app that changes data needs protection. Check that every state-changing endpoint is covered.