using Fido2NetLib; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.EntityFrameworkCore; using Cloud; using Cloud.Components; using Shared.Pages; using Shared.Services; var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddRazorComponents().AddInteractiveServerComponents(); builder.Services.AddTelerikBlazor(); builder.Services.AddSingleton(); builder.Services.AddScoped(); builder.Services.AddHttpClient(); builder.Services.AddDistributedMemoryCache(); builder.Services.AddSession(options => { options.IdleTimeout = TimeSpan.FromMinutes(5); options.Cookie.HttpOnly = true; options.Cookie.IsEssential = true; options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest; }); builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) .AddCookie(options => { options.LoginPath = "/login"; options.LogoutPath = "/logout"; options.ExpireTimeSpan = TimeSpan.FromDays(30); options.SlidingExpiration = true; options.Cookie.HttpOnly = true; options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest; options.Cookie.SameSite = SameSiteMode.Strict; }); builder.Services.AddAuthorization(); builder.Services.AddFido2(options => { options.ServerDomain = builder.Configuration["Fido2:ServerDomain"]!; options.ServerName = builder.Configuration["Fido2:ServerName"]!; options.Origins = builder.Configuration.GetSection("Fido2:Origins").Get>()!; options.TimestampDriftTolerance = 300_000; }); var connectionString = builder.Configuration.GetConnectionString("DefaultConnection"); builder.Services.AddDbContext(options => { if (!string.IsNullOrEmpty(connectionString)) options.UseNpgsql(connectionString); }); var app = builder.Build(); using (var scope = app.Services.CreateScope()) { try { var db = scope.ServiceProvider.GetRequiredService(); if (!string.IsNullOrEmpty(connectionString)) db.Database.Migrate(); } catch (Exception ex) { Console.WriteLine( $"[WARNING] Database migration failed: {ex.Message}. The application will continue without a database connection."); } } if (!app.Environment.IsDevelopment()) app.UseExceptionHandler("/Error"); app.UseStaticFiles(); app.UseSession(); app.UseAuthentication(); app.UseAuthorization(); app.UseAntiforgery(); app.MapControllers(); app.MapRazorComponents() .AddInteractiveServerRenderMode() .AddAdditionalAssemblies(typeof(Home).Assembly); app.Run();