- 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.
74 lines
2.8 KiB
C#
74 lines
2.8 KiB
C#
using DbFirst.BlazorWebApp;
|
|
using DbFirst.BlazorWebApp.Components;
|
|
using DbFirst.BlazorWebApp.Services;
|
|
using DevExpress.Blazor;
|
|
using System.Net;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// Add services to the container.
|
|
builder.Services.AddRazorComponents()
|
|
.AddInteractiveServerComponents();
|
|
|
|
builder.Services.AddDevExpressBlazor(options => options.BootstrapVersion = BootstrapVersion.v5);
|
|
builder.Services.AddScoped<ThemeState>();
|
|
builder.Services.AddScoped<BandLayoutService>();
|
|
builder.Services.AddScoped<AuthService>();
|
|
builder.Services.AddScoped<CookieContainer>();
|
|
|
|
builder.Services.Configure<AppSettings>(builder.Configuration);
|
|
var appSettings = builder.Configuration.Get<AppSettings>() ?? new AppSettings();
|
|
|
|
// Alle API-Clients teilen sich denselben scoped CookieContainer (pro Blazor-Circuit),
|
|
// damit das Auth-Cookie nach dem Login automatisch an alle Folgeanfragen angehängt wird.
|
|
static HttpClient CreateHttpClientWithCookies(CookieContainer cookieContainer, string? baseUrl)
|
|
{
|
|
var handler = new HttpClientHandler { CookieContainer = cookieContainer, UseCookies = true };
|
|
var client = new HttpClient(handler);
|
|
if (!string.IsNullOrWhiteSpace(baseUrl))
|
|
client.BaseAddress = new Uri(baseUrl);
|
|
return client;
|
|
}
|
|
|
|
builder.Services.AddScoped<IAuthApiClient>(sp =>
|
|
{
|
|
var cc = sp.GetRequiredService<CookieContainer>();
|
|
var client = CreateHttpClientWithCookies(cc, appSettings.ApiBaseUrl);
|
|
return new AuthApiClient(client, sp.GetRequiredService<AuthService>(), cc);
|
|
});
|
|
|
|
var dataApiBaseUrl = !string.IsNullOrWhiteSpace(appSettings.DataApiBaseUrl)
|
|
? appSettings.DataApiBaseUrl
|
|
: appSettings.ApiBaseUrl;
|
|
|
|
builder.Services.AddScoped<ICatalogApiClient>(sp =>
|
|
new CatalogApiClient(CreateHttpClientWithCookies(sp.GetRequiredService<CookieContainer>(), dataApiBaseUrl)));
|
|
|
|
builder.Services.AddScoped<IDashboardApiClient>(sp =>
|
|
new DashboardApiClient(CreateHttpClientWithCookies(sp.GetRequiredService<CookieContainer>(), dataApiBaseUrl)));
|
|
|
|
builder.Services.AddScoped<IMassDataApiClient>(sp =>
|
|
new MassDataApiClient(CreateHttpClientWithCookies(sp.GetRequiredService<CookieContainer>(), dataApiBaseUrl)));
|
|
|
|
builder.Services.AddScoped<ILayoutApiClient>(sp =>
|
|
new LayoutApiClient(CreateHttpClientWithCookies(sp.GetRequiredService<CookieContainer>(), dataApiBaseUrl)));
|
|
|
|
var app = builder.Build();
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (!app.Environment.IsDevelopment())
|
|
{
|
|
app.UseExceptionHandler("/Error", createScopeForErrors: true);
|
|
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
|
|
app.UseHsts();
|
|
}
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
app.UseStaticFiles();
|
|
app.UseAntiforgery();
|
|
|
|
app.MapRazorComponents<App>()
|
|
.AddInteractiveServerRenderMode();
|
|
|
|
app.Run(); |