The following example applies antiforgery request validation to the DevExpress ASP.NET Core Dashboard control.
Follow the steps below to apply antiforgery request validation.
- Create a custom dashboard controller. If you already have a custom controller, you can skip this step.
namespace AspNetCoreDashboardPreventCrossSiteRequestForgery.Controllers {
public class CustomDashboardController : DashboardController {
public CustomDashboardController(CustomDashboardConfigurator configurator, IDataProtectionProvider dataProtectionProvider = null): base(configurator, dataProtectionProvider) {
}
}
}
- Change default routing to use the created controller.
app.UseEndpoints(endpoints => {
endpoints.MapDashboardRoute("dashboardControl", "CustomDashboard");
// ...
});
- Specify the controller name in the Web Dashboard settings.
@(Html.DevExpress().Dashboard("dashboardControl1")
...
.ControllerName("CustomDashboard")
)
- Add the
Antiforgery
service.
services.AddAntiforgery(options => {
// Set Cookie properties using CookieBuilder properties†.
options.FormFieldName = "X-CSRF-TOKEN";
options.HeaderName = "X-CSRF-TOKEN";
options.SuppressXFrameOptionsHeader = false;
});
- Add the
AutoValidateAntiforgeryToken
attribute to the custom controller.
[AutoValidateAntiforgeryToken]
public class CustomDashboardController : DashboardController {
// ...
}
- Configure the Web Dashboard control's backend options.
@inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf
@(Html.DevExpress().Dashboard("dashboardControl1")
...
.ControllerName("CustomDashboard")
.BackendOptions(backendOptions => {
backendOptions.RequestHttpHeaders(headers => {
headers.Add("X-CSRF-TOKEN", Xsrf.GetAndStoreTokens(HttpContext).RequestToken);
});
})
)
- Web Dashboard - Security Considerations
- Prevent Cross-Site Request Forgery (XSRF/CSRF) attacks in ASP.NET Core
- CA3147: Mark verb handlers with ValidateAntiForgeryToken
(you will be redirected to DevExpress.com to submit your response)