Add cookie/JWT-based authentication and user context headers

Introduce a new authentication mechanism using JWT tokens stored in cookies, with a custom CookieAuthHandler for API request authentication. Add AuthServiceSettings for configuration and UserHeaderHandler to propagate user context in outgoing HTTP requests. Update service registrations and configuration files to support the new authentication flow. Refactor CurrentUserService for simplicity. This enables stateless, cookie-based authentication and consistent user context across API calls.
This commit is contained in:
OlgunR
2026-05-13 13:46:45 +02:00
parent de5d1b666c
commit ed3e7d4043
8 changed files with 224 additions and 42 deletions

View File

@@ -0,0 +1,17 @@
namespace DbFirst.BlazorWebApp.Services;
public class UserHeaderHandler(AuthService authService) : DelegatingHandler
{
protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
if (authService.IsAuthenticated)
{
request.Headers.TryAddWithoutValidation("X-Authenticated-User", authService.UserName);
if (!string.IsNullOrEmpty(authService.RawCookieHeader))
request.Headers.TryAddWithoutValidation("Cookie", authService.RawCookieHeader);
}
return base.SendAsync(request, cancellationToken);
}
}