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:
@@ -31,6 +31,18 @@
|
||||
else
|
||||
document.documentElement.classList.remove('dx-dark');
|
||||
};
|
||||
|
||||
window.authStorage = {
|
||||
get: function (key) { return sessionStorage.getItem(key); },
|
||||
set: function (username, cookie) {
|
||||
sessionStorage.setItem('auth_user', username);
|
||||
sessionStorage.setItem('auth_cookie', cookie);
|
||||
},
|
||||
clear: function () {
|
||||
sessionStorage.removeItem('auth_user');
|
||||
sessionStorage.removeItem('auth_cookie');
|
||||
}
|
||||
};
|
||||
</script>
|
||||
<HeadOutlet />
|
||||
</head>
|
||||
|
||||
10
DbFirst.BlazorWebApp/Components/Layout/LoginLayout.razor
Normal file
10
DbFirst.BlazorWebApp/Components/Layout/LoginLayout.razor
Normal file
@@ -0,0 +1,10 @@
|
||||
@inherits LayoutComponentBase
|
||||
@inject ThemeState ThemeState
|
||||
|
||||
<div class="page @(ThemeState.IsDarkMode ? "app-dark" : "app-light") @(ThemeState.IsNativeDarkTheme ? "native-dark" : "")">
|
||||
<main>
|
||||
<article class="content">
|
||||
@Body
|
||||
</article>
|
||||
</main>
|
||||
</div>
|
||||
@@ -2,6 +2,9 @@
|
||||
@implements IDisposable
|
||||
@inject ThemeState ThemeState
|
||||
@inject IJSRuntime JS
|
||||
@inject AuthService AuthService
|
||||
@inject IAuthApiClient AuthApiClient
|
||||
@inject NavigationManager Navigation
|
||||
|
||||
<div class="page @(ThemeState.IsDarkMode ? "app-dark" : "app-light") @(ThemeState.IsNativeDarkTheme ? "native-dark" : "")">
|
||||
<div class="sidebar">
|
||||
@@ -18,11 +21,29 @@
|
||||
<DxButton Text="@(ThemeState.IsDarkMode ? "Dark Mode aus" : "Dark Mode an")"
|
||||
Click="ToggleTheme" />
|
||||
</span>
|
||||
@if (AuthService.IsAuthenticated)
|
||||
{
|
||||
<span class="ms-auto d-flex align-items-center gap-2">
|
||||
<span class="top-row-username">@AuthService.UserName</span>
|
||||
<DxButton Text="Abmelden"
|
||||
Click="LogoutAsync"
|
||||
RenderStyle="ButtonRenderStyle.Secondary" />
|
||||
</span>
|
||||
}
|
||||
<a href="https://learn.microsoft.com/aspnet/core/" target="_blank">About</a>
|
||||
</div>
|
||||
|
||||
<article class="content px-4">
|
||||
@Body
|
||||
@if (_checkingAuth)
|
||||
{
|
||||
<div class="loading-container">
|
||||
<span>Wird geladen…</span>
|
||||
</div>
|
||||
}
|
||||
else
|
||||
{
|
||||
@Body
|
||||
}
|
||||
</article>
|
||||
</main>
|
||||
</div>
|
||||
@@ -35,10 +56,12 @@
|
||||
|
||||
@code {
|
||||
private bool _isInteractive;
|
||||
private bool _checkingAuth = true;
|
||||
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
ThemeState.OnChange += OnThemeChanged;
|
||||
AuthService.OnChange += OnAuthChanged;
|
||||
}
|
||||
|
||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||
@@ -46,17 +69,71 @@
|
||||
if (firstRender)
|
||||
{
|
||||
_isInteractive = true;
|
||||
|
||||
if (!AuthService.IsAuthenticated)
|
||||
{
|
||||
try
|
||||
{
|
||||
var username = await JS.InvokeAsync<string?>("authStorage.get", "auth_user");
|
||||
var cookie = await JS.InvokeAsync<string?>("authStorage.get", "auth_cookie");
|
||||
|
||||
if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(cookie))
|
||||
{
|
||||
var restored = await AuthApiClient.RestoreAsync(username, cookie);
|
||||
if (!restored)
|
||||
{
|
||||
await JS.InvokeVoidAsync("authStorage.clear");
|
||||
Navigation.NavigateTo("/login", replace: true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Navigation.NavigateTo("/login", replace: true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
Navigation.NavigateTo("/login", replace: true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
_checkingAuth = false;
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
await ApplyDxDarkOverrideAsync();
|
||||
}
|
||||
|
||||
private async Task LogoutAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
await AuthApiClient.LogoutAsync();
|
||||
await JS.InvokeVoidAsync("authStorage.clear");
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Fehler beim Abmelden ignorieren, trotzdem weiterleiten
|
||||
}
|
||||
|
||||
Navigation.NavigateTo("/login", replace: true);
|
||||
}
|
||||
|
||||
private void OnAuthChanged()
|
||||
{
|
||||
InvokeAsync(StateHasChanged);
|
||||
}
|
||||
|
||||
private void OnThemeChanged()
|
||||
{
|
||||
InvokeAsync(async () =>
|
||||
{
|
||||
StateHasChanged();
|
||||
if (_isInteractive)
|
||||
await ApplyDxDarkOverrideAsync();
|
||||
await ApplyDxDarkOverrideAsync();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -82,5 +159,6 @@
|
||||
public void Dispose()
|
||||
{
|
||||
ThemeState.OnChange -= OnThemeChanged;
|
||||
AuthService.OnChange -= OnAuthChanged;
|
||||
}
|
||||
}
|
||||
|
||||
106
DbFirst.BlazorWebApp/Components/Pages/Login.razor
Normal file
106
DbFirst.BlazorWebApp/Components/Pages/Login.razor
Normal file
@@ -0,0 +1,106 @@
|
||||
@page "/login"
|
||||
@layout DbFirst.BlazorWebApp.Components.Layout.LoginLayout
|
||||
@rendermode InteractiveServer
|
||||
|
||||
@inject IAuthApiClient AuthApiClient
|
||||
@inject AuthService AuthService
|
||||
@inject NavigationManager Navigation
|
||||
@inject IJSRuntime JS
|
||||
|
||||
<PageTitle>Anmelden – DbFirst</PageTitle>
|
||||
|
||||
<div class="login-container">
|
||||
<div class="login-card">
|
||||
<div class="login-header">
|
||||
<span class="login-brand">DbFirst</span>
|
||||
<p class="login-subtitle">Bitte melden Sie sich an</p>
|
||||
</div>
|
||||
|
||||
<form @onsubmit="HandleLoginAsync" @onsubmit:preventDefault>
|
||||
<div class="login-field">
|
||||
<label class="login-label" for="username">Benutzername</label>
|
||||
<input id="username"
|
||||
type="text"
|
||||
class="login-text-input"
|
||||
placeholder="Benutzername eingeben"
|
||||
autocomplete="username"
|
||||
@bind="_username"
|
||||
@bind:event="oninput" />
|
||||
</div>
|
||||
|
||||
<div class="login-field">
|
||||
<label class="login-label" for="password">Passwort</label>
|
||||
<input id="password"
|
||||
type="password"
|
||||
class="login-text-input"
|
||||
placeholder="Passwort eingeben"
|
||||
autocomplete="current-password"
|
||||
@bind="_password"
|
||||
@bind:event="oninput" />
|
||||
</div>
|
||||
|
||||
@if (!string.IsNullOrEmpty(_errorMessage))
|
||||
{
|
||||
<div class="alert alert-danger mt-2 mb-1" role="alert">
|
||||
@_errorMessage
|
||||
</div>
|
||||
}
|
||||
|
||||
<div class="login-actions">
|
||||
<button type="submit"
|
||||
class="login-submit-btn"
|
||||
disabled="@_isLoading">
|
||||
@(_isLoading ? "Wird angemeldet…" : "Anmelden")
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@code {
|
||||
private string _username = string.Empty;
|
||||
private string _password = string.Empty;
|
||||
private string _errorMessage = string.Empty;
|
||||
private bool _isLoading;
|
||||
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
if (AuthService.IsAuthenticated)
|
||||
Navigation.NavigateTo("/");
|
||||
}
|
||||
|
||||
private async Task HandleLoginAsync()
|
||||
{
|
||||
if (_isLoading) return;
|
||||
_errorMessage = string.Empty;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(_username) || string.IsNullOrWhiteSpace(_password))
|
||||
{
|
||||
_errorMessage = "Bitte Benutzername und Passwort eingeben.";
|
||||
return;
|
||||
}
|
||||
|
||||
_isLoading = true;
|
||||
try
|
||||
{
|
||||
var success = await AuthApiClient.LoginAsync(_username, _password);
|
||||
if (success)
|
||||
{
|
||||
await JS.InvokeVoidAsync("authStorage.set", AuthService.UserName, AuthService.RawCookieHeader);
|
||||
Navigation.NavigateTo("/");
|
||||
}
|
||||
else
|
||||
{
|
||||
_errorMessage = "Anmeldung fehlgeschlagen. Bitte Benutzerdaten prüfen.";
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
_errorMessage = "Verbindungsfehler. Bitte später erneut versuchen.";
|
||||
}
|
||||
finally
|
||||
{
|
||||
_isLoading = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
111
DbFirst.BlazorWebApp/Components/Pages/Login.razor.css
Normal file
111
DbFirst.BlazorWebApp/Components/Pages/Login.razor.css
Normal file
@@ -0,0 +1,111 @@
|
||||
.login-container {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
min-height: calc(100vh - 2rem);
|
||||
}
|
||||
|
||||
.login-card {
|
||||
width: 100%;
|
||||
max-width: 400px;
|
||||
padding: 2rem;
|
||||
border-radius: 8px;
|
||||
border: 1px solid var(--band-editor-border, #dee2e6);
|
||||
background-color: var(--band-editor-bg, #fff);
|
||||
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
|
||||
}
|
||||
|
||||
.login-header {
|
||||
text-align: center;
|
||||
margin-bottom: 1.75rem;
|
||||
}
|
||||
|
||||
.login-brand {
|
||||
font-size: 1.8rem;
|
||||
font-weight: 700;
|
||||
color: #1b6ec2;
|
||||
letter-spacing: 0.02em;
|
||||
}
|
||||
|
||||
.login-subtitle {
|
||||
margin-top: 0.35rem;
|
||||
margin-bottom: 0;
|
||||
font-size: 0.9rem;
|
||||
color: #6c757d;
|
||||
}
|
||||
|
||||
.login-field {
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.login-label {
|
||||
display: block;
|
||||
font-size: 0.85rem;
|
||||
font-weight: 500;
|
||||
margin-bottom: 0.3rem;
|
||||
}
|
||||
|
||||
.login-actions {
|
||||
margin-top: 1.25rem;
|
||||
}
|
||||
|
||||
/* Gemeinsamer Stil für Text- und Passwort-Eingabefelder */
|
||||
.login-text-input {
|
||||
display: block;
|
||||
width: 100%;
|
||||
height: 2.25rem;
|
||||
padding: 0.25rem 0.65rem;
|
||||
font-size: 1rem;
|
||||
font-family: inherit;
|
||||
line-height: 1.5;
|
||||
color: inherit;
|
||||
background-color: transparent;
|
||||
border: 1px solid #ced4da;
|
||||
border-radius: 4px;
|
||||
box-sizing: border-box;
|
||||
outline: none;
|
||||
transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
|
||||
}
|
||||
|
||||
.login-text-input:focus {
|
||||
border-color: #86b7fe;
|
||||
box-shadow: 0 0 0 0.2rem rgba(13, 110, 253, 0.25);
|
||||
}
|
||||
|
||||
/* Submit-Button */
|
||||
.login-submit-btn {
|
||||
display: block;
|
||||
width: 100%;
|
||||
padding: 0.45rem 1rem;
|
||||
font-size: 1rem;
|
||||
font-family: inherit;
|
||||
font-weight: 500;
|
||||
color: #fff;
|
||||
background-color: #1b6ec2;
|
||||
border: 1px solid #1861ac;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
transition: background-color 0.15s ease-in-out, border-color 0.15s ease-in-out;
|
||||
}
|
||||
|
||||
.login-submit-btn:hover:not(:disabled) {
|
||||
background-color: #1558a0;
|
||||
border-color: #1456a0;
|
||||
}
|
||||
|
||||
.login-submit-btn:disabled {
|
||||
opacity: 0.65;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
/* Dark-Mode */
|
||||
.app-dark .login-text-input {
|
||||
border-color: #555;
|
||||
color: #e8e8e8;
|
||||
background-color: #2d2d2d;
|
||||
}
|
||||
|
||||
.app-dark .login-text-input:focus {
|
||||
border-color: #6cb6ff;
|
||||
box-shadow: 0 0 0 0.2rem rgba(108, 182, 255, 0.25);
|
||||
}
|
||||
Reference in New Issue
Block a user