fix(web): appels API du circuit authentifiés après reload — cookie SSR propagé, purge au login/logout (Fixes #45)
This commit is contained in:
1 parent
29ce8bd897
commit
c2ff225e59
6 files changed
+149
-102
No files matched your search
@@ -114,15 +114,18 @@ public class SameOriginHttpMessageHandler : DelegatingHandler
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly IServer _server;
|
||||
private readonly AuthState _authState;
|
||||
|
||||
public SameOriginHttpMessageHandler(
|
||||
IHttpContextAccessor httpContextAccessor,
|
||||
IConfiguration configuration,
|
||||
IServer server)
|
||||
IServer server,
|
||||
AuthState authState)
|
||||
{
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
_configuration = configuration;
|
||||
_server = server;
|
||||
_authState = authState;
|
||||
}
|
||||
|
||||
protected override Task<HttpResponseMessage> SendAsync(
|
||||
@@ -158,6 +161,19 @@ public class SameOriginHttpMessageHandler : DelegatingHandler
|
||||
if (!string.IsNullOrWhiteSpace(cookie))
|
||||
request.Headers.TryAddWithoutValidation("Cookie", cookie);
|
||||
}
|
||||
// Circuit Blazor (HttpContext null) : le navigateur ne transmet pas le
|
||||
// cookie au serveur par le circuit, et AccessToken peut être null quand
|
||||
// le sessionStorage est vide (reload) — on réutilise alors le cookie
|
||||
// capturé lors de la restauration SSR (AuthState.AuthCookie) pour que
|
||||
// les appels API du circuit restent authentifiés (sinon 302 en boucle).
|
||||
else if (http is null
|
||||
&& request.Headers.Authorization is null
|
||||
&& !request.Headers.Contains("Cookie")
|
||||
&& !string.IsNullOrEmpty(_authState.AuthCookie))
|
||||
{
|
||||
request.Headers.TryAddWithoutValidation("Cookie",
|
||||
$"{AuthState.AuthCookieName}={_authState.AuthCookie}");
|
||||
}
|
||||
|
||||
return base.SendAsync(request, cancellationToken);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user