aspnetmvc-405006-security-considerations-prevent-csrf.md
In Cross-Site Request Forgery (CSRF) attacks, a threat actor tricks an authenticated user into executing unauthorized commands.
Use anti-forgery tokens to protect your application from CSRF attacks. These tokens work as follows:
Note
Do not use the GET method to send data modifying requests.
To add anti-forgery tokens to a Razor page:
In the view code, generate an AntiForgery token for a form:
Make certain that AJAX requests for all controls include the token value:
To validate the request token on the server, apply the ValidateAntiForgeryToken attribute to the corresponding controller action:
Note
The following steps supplement, but do not replace, the anti‑forgery token validation for page-level callbacks described above. Implement both mechanisms for complete CSRF protection.
To protect against CSRF attacks when using the Dashboard Designer:
Inject an anti-forgery token into the Dashboard’s AJAX request header as follows:
Define a custom attribute to validate the anti-forgery token on requests:
Apply this attribute to the controller action that handles Dashboard callbacks:
To protect against CSRF attacks when using File Manager, File Upload, Rich Text Editor, and Spreadsheet controls:
Generate an anti-forgery token on a page:
Add a client function that injects the token into the RequestVerificationToken HTTP request header.
Pass the function name to the control’s BeforeSend client-side event to add the token to all HTTP requests.
Implement IHttpModuleSubscriber to validate the RequestVerificationToken HTTP request header against the anti‑forgery cookie token. Reject all unsafe requests that fail validation.
Register the AntiforgeryOfficeHttpModuleSubscriber at application startup so the DevExpress module can invoke it for matching requests.
See Also
Preventing Cross-Site Request Forgery (CSRF) Attacks in ASP.NET MVC Application