Wire ASP.NET Core Identity DI alongside existing AuthRepository

Both stacks coexist until handlers are migrated one by one.
This commit is contained in:
Aaron Po
2026-08-22 01:29:40 -04:00
parent 3c0ca485b8
commit f06f4bf3a9

View File

@@ -1,6 +1,8 @@
using Features.Auth.Identity;
using Features.Auth.Repository;
using Features.Auth.Services;
using Infrastructure.PasswordHashing;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
namespace Features.Auth.DependencyInjection;
@@ -9,14 +11,43 @@ namespace Features.Auth.DependencyInjection;
public static class FeaturesAuthServiceCollectionExtensions
{
/// <summary>
/// Adds <see cref="IAuthRepository" />, <see cref="ITokenService" />, and the Argon2 password hashing
/// infrastructure as scoped services.
/// Adds <see cref="IAuthRepository" />, <see cref="ITokenService" />, the Argon2 password hashing
/// infrastructure, and ASP.NET Core Identity (<see cref="UserManager{TUser}" /> backed by
/// <see cref="DapperUserStore" />) as scoped services.
/// </summary>
public static IServiceCollection AddFeaturesAuth(this IServiceCollection services)
{
services.AddScoped<IAuthRepository, AuthRepository>();
services.AddScoped<ITokenService, TokenService>();
services.AddScoped<IPasswordInfrastructure, Argon2Infrastructure>();
services
.AddIdentityCore<ApplicationUser>(options =>
{
options.User.RequireUniqueEmail = true;
// Password/username complexity is already enforced by RegisterUserValidator
// (FluentValidation); disable Identity's own checks to avoid duplicated/drifting rules.
options.Password.RequireDigit = false;
options.Password.RequireLowercase = false;
options.Password.RequireUppercase = false;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequiredLength = 1;
})
.AddDefaultTokenProviders();
services.AddScoped<DapperUserStore>();
services.AddScoped<IUserStore<ApplicationUser>>(sp =>
sp.GetRequiredService<DapperUserStore>()
);
services.AddScoped<IUserEmailStore<ApplicationUser>>(sp =>
sp.GetRequiredService<DapperUserStore>()
);
services.AddScoped<IUserPasswordStore<ApplicationUser>>(sp =>
sp.GetRequiredService<DapperUserStore>()
);
services.AddScoped<IPasswordHasher<ApplicationUser>, Argon2PasswordHasher>();
return services;
}
}