using Eshop.Application.Auth; using Eshop.Infrastructure.Identity; using Eshop.Infrastructure.Persistence; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; namespace Eshop.Infrastructure; public static class DependencyInjection { public static IServiceCollection AddInfrastructure(this IServiceCollection services) => AddApplicationServices(services); public static IServiceCollection AddInfrastructure( this IServiceCollection services, IConfiguration configuration) { var connectionString = configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found."); services.AddDbContext(options => options.UseNpgsql(connectionString)); var pwSection = configuration.GetSection("Identity:Password"); services.AddIdentity(options => { options.Password.RequiredLength = pwSection.GetValue("RequiredLength", 6); options.Password.RequireDigit = pwSection.GetValue("RequireDigit", true); options.Password.RequireLowercase = pwSection.GetValue("RequireLowercase", true); options.Password.RequireUppercase = pwSection.GetValue("RequireUppercase", true); options.Password.RequireNonAlphanumeric = pwSection.GetValue("RequireNonAlphanumeric", false); options.Password.RequiredUniqueChars = pwSection.GetValue("RequiredUniqueChars", 1); }) .AddEntityFrameworkStores() .AddDefaultTokenProviders(); services.ConfigureApplicationCookie(options => { options.LoginPath = "/Auth/Login"; options.AccessDeniedPath = "/Auth/AccessDenied"; options.ExpireTimeSpan = TimeSpan.FromHours(2); }); return AddApplicationServices(services); } private static IServiceCollection AddApplicationServices(IServiceCollection services) { services.AddScoped(); services.AddScoped(); services.AddScoped(); return services; } }