From f7c5f7a80a9650137f9bef81d3a20128fbb420bd Mon Sep 17 00:00:00 2001 From: Arnaud Dartois Date: Tue, 21 Jul 2026 07:30:14 +0000 Subject: [PATCH] fix: move UseAntiforgery after UseAuthorization MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/Heimdall.Web/Program.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Heimdall.Web/Program.cs b/src/Heimdall.Web/Program.cs index 044e2db..2716aa7 100644 --- a/src/Heimdall.Web/Program.cs +++ b/src/Heimdall.Web/Program.cs @@ -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() .AddInteractiveServerRenderMode();