2021-11-19 16:11:12 +01:00
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
|
|
|
|
|
2021-11-20 13:52:28 +01:00
|
|
|
namespace zero.Identity;
|
2021-11-19 16:11:12 +01:00
|
|
|
|
|
|
|
|
public static class ZeroIdentityExtensions
|
|
|
|
|
{
|
|
|
|
|
public static IdentityBuilder AddZeroIdentity<TUser, TRole>(this IServiceCollection services)
|
2021-12-26 01:42:21 +01:00
|
|
|
where TUser : ZeroIdentityUser, new()
|
|
|
|
|
where TRole : ZeroIdentityRole, new()
|
2021-11-19 16:11:12 +01:00
|
|
|
{
|
|
|
|
|
services.AddZeroIdentityCore<TUser>();
|
|
|
|
|
|
2021-11-29 00:38:55 +01:00
|
|
|
IdentityBuilder builder = new(typeof(TUser), typeof(TRole), services);
|
2021-11-19 16:11:12 +01:00
|
|
|
|
|
|
|
|
builder.AddDefaultTokenProviders();
|
|
|
|
|
|
|
|
|
|
builder.AddUserStore<RavenUserStore<TUser, TRole>>();
|
|
|
|
|
builder.AddSignInManager<SchemedSignInManager<TUser>>();
|
|
|
|
|
builder.AddClaimsPrincipalFactory<ZeroClaimsPrinicipalFactory<TUser, TRole>>();
|
|
|
|
|
|
|
|
|
|
builder.AddRoleValidator<RoleValidator<TRole>>();
|
|
|
|
|
builder.AddRoleManager<RoleManager<TRole>>();
|
|
|
|
|
builder.AddRoleStore<RavenRoleStore<TRole>>();
|
|
|
|
|
|
|
|
|
|
return builder;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static IdentityBuilder AddZeroIdentity<TUser>(this IServiceCollection services)
|
2021-12-26 01:42:21 +01:00
|
|
|
where TUser : ZeroIdentityUser, new()
|
2021-11-19 16:11:12 +01:00
|
|
|
{
|
|
|
|
|
services.AddZeroIdentityCore<TUser>();
|
|
|
|
|
|
2021-11-29 00:38:55 +01:00
|
|
|
IdentityBuilder builder = new(typeof(TUser), services);
|
2021-11-19 16:11:12 +01:00
|
|
|
|
|
|
|
|
builder.AddDefaultTokenProviders();
|
|
|
|
|
builder.AddUserStore<RavenUserStore<TUser>>();
|
|
|
|
|
builder.AddSignInManager<SchemedSignInManager<TUser>>();
|
|
|
|
|
builder.AddClaimsPrincipalFactory<ZeroClaimsPrinicipalFactory<TUser>>();
|
|
|
|
|
|
|
|
|
|
return builder;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static IServiceCollection AddZeroIdentityCore<TUser>(this IServiceCollection services)
|
2021-12-26 01:42:21 +01:00
|
|
|
where TUser : ZeroIdentityUser, new()
|
2021-11-19 16:11:12 +01:00
|
|
|
{
|
|
|
|
|
services.AddHttpContextAccessor();
|
|
|
|
|
services.AddOptions();
|
|
|
|
|
services.AddLogging();
|
|
|
|
|
|
|
|
|
|
services.TryAddScoped<IUserValidator<TUser>, UserValidator<TUser>>();
|
|
|
|
|
services.TryAddScoped<IPasswordValidator<TUser>, PasswordValidator<TUser>>();
|
|
|
|
|
services.TryAddScoped<IPasswordHasher<TUser>, PasswordHasher<TUser>>();
|
|
|
|
|
services.TryAddScoped<ILookupNormalizer, UpperInvariantLookupNormalizer>();
|
|
|
|
|
services.TryAddScoped<IUserConfirmation<TUser>, DefaultUserConfirmation<TUser>>();
|
|
|
|
|
services.TryAddScoped<IdentityErrorDescriber>();
|
|
|
|
|
services.TryAddScoped<UserManager<TUser>>();
|
|
|
|
|
|
|
|
|
|
services.Configure<IdentityOptions>(opts =>
|
|
|
|
|
{
|
|
|
|
|
opts.ClaimsIdentity.UserIdClaimType = Constants.Auth.Claims.UserId;
|
|
|
|
|
opts.ClaimsIdentity.UserNameClaimType = Constants.Auth.Claims.UserName;
|
|
|
|
|
opts.ClaimsIdentity.RoleClaimType = Constants.Auth.Claims.Role;
|
|
|
|
|
opts.ClaimsIdentity.SecurityStampClaimType = Constants.Auth.Claims.SecurityStamp;
|
|
|
|
|
opts.ClaimsIdentity.EmailClaimType = Constants.Auth.Claims.Email;
|
|
|
|
|
|
|
|
|
|
opts.Password.RequireDigit = false;
|
|
|
|
|
opts.Password.RequireLowercase = false;
|
|
|
|
|
opts.Password.RequireUppercase = false;
|
|
|
|
|
opts.Password.RequireNonAlphanumeric = false;
|
|
|
|
|
opts.Password.RequiredLength = 8;
|
|
|
|
|
opts.Password.RequiredUniqueChars = 1;
|
|
|
|
|
|
|
|
|
|
opts.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(60);
|
|
|
|
|
opts.Lockout.MaxFailedAccessAttempts = 5;
|
|
|
|
|
opts.Lockout.AllowedForNewUsers = true;
|
|
|
|
|
|
|
|
|
|
opts.User.RequireUniqueEmail = true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
services.Configure<SecurityStampValidatorOptions>(opts =>
|
|
|
|
|
{
|
|
|
|
|
opts.ValidationInterval = TimeSpan.FromMinutes(90);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
}
|