Skip to content

Instantly share code, notes, and snippets.

@Lavshyak
Last active November 9, 2024 14:46
Show Gist options
  • Select an option

  • Save Lavshyak/52e817ae0a5ee4c52d9ed4b0495186db to your computer and use it in GitHub Desktop.

Select an option

Save Lavshyak/52e817ae0a5ee4c52d9ed4b0495186db to your computer and use it in GitHub Desktop.
asp.net 8 add identity template
// (for identity
services.AddHttpContextAccessor();
services.TryAddScoped<IdentityErrorDescriber>();
services.TryAddEnumerable(ServiceDescriptor
.Singleton<IPostConfigureOptions<SecurityStampValidatorOptions>,
MyPostConfigureSecurityStampValidatorOptions>());
services.TryAddScoped<ILookupNormalizer, UpperInvariantLookupNormalizer>();
// )
services.AddIdentityForAccount();
private sealed class
MyPostConfigureSecurityStampValidatorOptions : IPostConfigureOptions<SecurityStampValidatorOptions>
{
public MyPostConfigureSecurityStampValidatorOptions(TimeProvider timeProvider)
{
TimeProvider = timeProvider;
}
private TimeProvider TimeProvider { get; }
public void PostConfigure(string? name, SecurityStampValidatorOptions options)
{
options.TimeProvider ??= TimeProvider;
}
}
private static IdentityBuilder AddIdentityForAccount<TUser,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
TRole>(
this IServiceCollection services,
Action<IdentityOptions>? setupAction)
where TUser : class
where TRole : class
{
services.TryAddScoped<IUserValidator<TUser>, UserValidator<TUser>>();
services.TryAddScoped<IPasswordValidator<TUser>, PasswordValidator<TUser>>();
services.TryAddScoped<IPasswordHasher<TUser>, PasswordHasher<TUser>>();
services.TryAddScoped<IRoleValidator<TRole>, RoleValidator<TRole>>();
services.TryAddScoped<SecurityStampValidator<TUser>>();
services.TryAddScoped<TwoFactorSecurityStampValidator<TUser>>();
services.TryAddScoped<IUserClaimsPrincipalFactory<TUser>, UserClaimsPrincipalFactory<TUser, TRole>>();
services.TryAddScoped<IUserConfirmation<TUser>, DefaultUserConfirmation<TUser>>();
services.TryAddScoped<UserManager<TUser>>();
//services.TryAddScoped<SignInManager<TUser>>();
services.TryAddScoped<RoleManager<TRole>>();
if (setupAction != null)
{
services.Configure(setupAction);
}
return new IdentityBuilder(typeof(TUser), typeof(TRole), services);
}
private static void AddIdentityForAccount(this IServiceCollection services)
{
services.AddIdentityForAccount<Account, AccountRole>(options =>
{
// Password settings
options.Password.RequireDigit = false;
options.Password.RequiredLength = 4;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
options.Password.RequireLowercase = false;
// Lockout settings
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30);
options.Lockout.MaxFailedAccessAttempts = 10;
options.Lockout.AllowedForNewUsers = true;
// User settings
options.User.RequireUniqueEmail = true;
options.SignIn.RequireConfirmedEmail = true;
var claimsIdentity = options.ClaimsIdentity;
claimsIdentity.UserIdClaimType = AccountClaimTypes.NameIdentifier;
claimsIdentity.UserNameClaimType = AccountClaimTypes.Name;
claimsIdentity.EmailClaimType = AccountClaimTypes.Email;
claimsIdentity.RoleClaimType = AccountClaimTypes.Role;
claimsIdentity.SecurityStampClaimType = AccountClaimTypes.SecurityStampClaimType;
}).AddSignInManager<AccountSignInManager>()
.AddEntityFrameworkStores<AccountDbContext>()
.AddDefaultTokenProviders();
services.AddScoped<SignInManager<Account>,AccountSignInManager>();
services.AddScoped<AccountSignInManager>();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment