using Koogle.Domain.Interfaces; using Koogle.Infrastrcuture.Services; using Koogle.Infrastructure.Data; using Koogle.Infrastructure.Identity; using Koogle.Infrastructure.Repositories; using Koogle.Infrastructure.Security; using Koogle.Infrastructure.Services; using KoogleApp.Data; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; namespace Koogle.Infrastructure; public static class DependencyInjection { public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration) { var conn = configuration.GetConnectionString("AppDb") ?? throw new InvalidOperationException("Connection string 'AppDb' not found."); services.AddDatabaseDeveloperPageExceptionFilter(); // Domain DbContext (app schema) services.AddDbContext(options => options.UseSqlServer(conn, sql => { // Eigene Migrations-History für Domain sql.MigrationsHistoryTable("__EFMigrationsHistory_App", "app"); })); // DbContext Factory for scoped contexts in services services.AddDbContextFactory(options => options.UseSqlServer(conn, sql => { sql.MigrationsHistoryTable("__EFMigrationsHistory_App", "app"); }), ServiceLifetime.Scoped); // Identity DbContext (auth schema) services.AddDbContext(options => options.UseSqlServer(conn, sql => { // Eigene Migrations-History für Identity sql.MigrationsHistoryTable("__EFMigrationsHistory_Auth", "auth"); })); // Identity Services (UserManager/SignInManager etc.) services .AddIdentity(options => { // Optionen (Passwort, Lockout etc.) – Identity lässt sich hier konfigurieren. [2](https://krahgruppe-my.sharepoint.com/personal/c_kauer_krah-gruppe_de1/Documents/Microsoft%20Copilot%20Chat-Dateien/Person.cs) }) .AddEntityFrameworkStores() .AddDefaultTokenProviders() .AddClaimsPrincipalFactory(); services.AddAuthentication("Cookies") .AddCookie("Cookies", options => { options.LoginPath = "/login"; options.AccessDeniedPath = "/login"; }); services.AddAuthorization(options => { options.AddPolicy("ClubViewer", p => p.Requirements.Add(new ClubRoleRequirement("Viewer"))); options.AddPolicy("ClubEditor", p => p.Requirements.Add(new ClubRoleRequirement("Editor"))); options.AddPolicy("ClubAdmin", p => p.Requirements.Add(new ClubRoleRequirement("Admin"))); }); services.AddSingleton(); services.AddSingleton(); // HTTP Context Accessor (für CurrentUserService) services.AddHttpContextAccessor(); // Current UserProfile Service services.AddScoped(); // Repositories services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); // Services services.AddScoped(); services.AddCascadingAuthenticationState(); return services; } }