fix: move UseAntiforgery after UseAuthorization

UseAntiforgery() was before UseAuthentication(), causing all POST
requests to API controllers to get 400 (missing CSRF token) before
auth could return 401. Moving it after authorization ensures:

1. Auth middleware runs first → unauthenticated requests get 401
2. Antiforgery middleware validates tokens only for requests
   that pass auth (Blazor components)
3. API controllers and SignalR hubs work without CSRF tokens
This commit is contained in:
Arnaud Dartois committed 2026-07-21 07:30:14 +00:00
1 parent 81b8e6aacf
commit f7c5f7a80a
1 file changed
+4 -1
+4 -1
View File
@@ -235,12 +235,15 @@ if (!app.Environment.IsDevelopment())
app.UseStatusCodePagesWithReExecute("/not-found", createScopeForStatusCodePages: true);
app.UseHttpsRedirection();
app.UseAntiforgery();
app.UseStaticFiles();
app.UseAuthentication();
app.UseAuthorization();
// Anti-forgery AFTER auth so unauthenticated API calls get 401
// before the anti-forgery middleware returns 400.
app.UseAntiforgery();
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode();