Umbraco Tip: How to securely fix "SurfaceController POST not allowed outside Umbraco due to missing antiforgery token"
Today, I was working on a v8 to v13 upgrade project, and I realised that I couldn't access my [HttpPost] actions in a SurfaceController from an Ajax POST call.
After trying some solutions without any luck, including Route attributes to the SurfaceController and actions, I found out that starting from Umbraco v9, the SurfaceControllers have the anti-forgery check by default as SurfaceControllers are primarily made for POSTing forms within Umbraco.
To resolve this issue, I added the following beforeSend bit to my Ajax call and also added the [ValidateAntiForgeryToken] attribute to my actions in my SurfaceController.
During tests, I also realised that I could ignore the anti-forgery token completely by adding the [IgnoreAntiforgeryToken] attribute to my actions, but this is not an option that anybody should go for as this option skips the anti-forgery token validation and makes your website more vulnerable for things like Cross-site request forgery attacks.
That's all. Big thanks to fellow Umbraco people who have contributed to this Umbraco GitHub issue: https://github.com/umbraco/UmbracoDocs/issues/3242
Comments
Post a Comment