using GhostImageManager.Components; using GhostImageManager.Models; using GhostImageManager.Services; using Microsoft.AspNetCore.Authentication.Cookies; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddRazorComponents() .AddInteractiveServerComponents(); builder.Services.Configure(builder.Configuration.GetSection("AppSettings")); builder.Services.AddSingleton(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddHttpClient(); // Authentication builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) .AddCookie(options => { options.LoginPath = "/login"; options.LogoutPath = "/logout"; options.AccessDeniedPath = "/access-denied"; options.ExpireTimeSpan = TimeSpan.FromMinutes(480); options.SlidingExpiration = true; }); builder.Services.AddAuthorization(); builder.Services.AddCascadingAuthenticationState(); // Upload-Limit erhöhen (für Bildtausch) builder.WebHost.ConfigureKestrel(options => { options.Limits.MaxRequestBodySize = 104857600; // 100 MB }); var app = builder.Build(); // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Error", createScopeForErrors: true); app.UseHsts(); } app.UseHttpsRedirection(); // Standard wwwroot app.UseStaticFiles(); // Zusätzlich: start.jpg aus dem App-Root ausliefern var rootImagePath = Path.Combine(app.Environment.ContentRootPath, "start.jpg"); if (File.Exists(rootImagePath)) { app.UseStaticFiles(new StaticFileOptions { FileProvider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(app.Environment.ContentRootPath), RequestPath = "/start-image" }); } app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseAntiforgery(); app.MapRazorComponents() .AddInteractiveServerRenderMode(); // Authentication API endpoints app.MapPost("/auth/login", async (HttpContext context, AuthService authService) => { var form = await context.Request.ReadFormAsync(); var password = form["password"].ToString(); var returnUrl = form["returnUrl"].ToString(); if (string.IsNullOrEmpty(returnUrl)) returnUrl = "/"; try { await authService.SignInAsync(context, password); return Results.Redirect(returnUrl); } catch (UnauthorizedAccessException) { return Results.Redirect($"/login?errorMessage={Uri.EscapeDataString("Ungültiges Passwort.")}&returnUrl={Uri.EscapeDataString(returnUrl)}"); } }); app.MapPost("/auth/logout", async (HttpContext context, AuthService authService) => { await authService.SignOutAsync(context); return Results.Redirect("/login"); }); app.Run();