generated from egmont11/ASP.Net-Core-MVC-Template
57 lines
2.5 KiB
C#
57 lines
2.5 KiB
C#
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<AppDbContext>(options =>
|
|
options.UseNpgsql(connectionString));
|
|
|
|
var pwSection = configuration.GetSection("Identity:Password");
|
|
services.AddIdentity<UserEntity, IdentityRole>(options =>
|
|
{
|
|
options.Password.RequiredLength = pwSection.GetValue<int>("RequiredLength", 6);
|
|
options.Password.RequireDigit = pwSection.GetValue<bool>("RequireDigit", true);
|
|
options.Password.RequireLowercase = pwSection.GetValue<bool>("RequireLowercase", true);
|
|
options.Password.RequireUppercase = pwSection.GetValue<bool>("RequireUppercase", true);
|
|
options.Password.RequireNonAlphanumeric = pwSection.GetValue<bool>("RequireNonAlphanumeric", false);
|
|
options.Password.RequiredUniqueChars = pwSection.GetValue<int>("RequiredUniqueChars", 1);
|
|
})
|
|
.AddEntityFrameworkStores<AppDbContext>()
|
|
.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<IAuthenticationService, AspNetIdentityAuthenticationService>();
|
|
services.AddScoped<IUserAccountService, AspNetIdentityUserAccountService>();
|
|
services.AddScoped<IUserAdministrationService, AspNetIdentityUserAdministrationService>();
|
|
return services;
|
|
}
|
|
}
|