Add authentication support with login/logout UI

- Introduced AuthService, IAuthApiClient, and AuthApiClient for managing authentication state and API calls (login, logout, session restore).
- Added Login.razor and LoginLayout.razor for the login page, including styling and logic.
- MainLayout.razor now checks authentication on load, restores sessions from sessionStorage, and redirects to /login if unauthenticated. Displays username and logout button when logged in.
- Implemented JS interop (authStorage) for persisting authentication info in sessionStorage.
- Registered AuthService, CookieContainer, and API clients in Program.cs to share cookies and support authentication.
- Updated AppSettings and appsettings files to support separate ApiBaseUrl and DataApiBaseUrl.
- Minor CSS improvements for username display in the top bar.
This commit is contained in:
OlgunR
2026-05-12 16:32:46 +02:00
parent 45011122b2
commit 1ad267e409
13 changed files with 462 additions and 11 deletions

View File

@@ -0,0 +1,26 @@
namespace DbFirst.BlazorWebApp.Services;
public class AuthService
{
public bool IsAuthenticated { get; private set; }
public string UserName { get; private set; } = string.Empty;
public string? RawCookieHeader { get; private set; }
public event Action? OnChange;
public void SetAuthenticated(string userName, string rawCookieHeader)
{
IsAuthenticated = true;
UserName = userName;
RawCookieHeader = rawCookieHeader;
OnChange?.Invoke();
}
public void SetUnauthenticated()
{
IsAuthenticated = false;
UserName = string.Empty;
RawCookieHeader = null;
OnChange?.Invoke();
}
}