using System; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Options; using Microsoft.AspNetCore.Identity; namespace AspNetCore.Identity.MongoDbCore { /// /// Represents the password hashing options /// public sealed class PasswordHasherOptionsAccessor : IOptions { /// /// Gets options which use the IdentityV3 compat mode, and set the iteration count to 200000 PBKDF2-SHA256 iterations /// (roughly 200ms of work) /// public PasswordHasherOptions Value { get; } = new PasswordHasherOptions { CompatibilityMode = PasswordHasherCompatibilityMode.IdentityV3, IterationCount = 200000 }; } public static class MongoIdentityServiceCollectionExtensions { public static IdentityBuilder AddIdentity(this IServiceCollection services) where TUser : class => services.AddIdentity(null); public static IdentityBuilder AddIdentity(this IServiceCollection services, Action setupAction) where TUser : class { // Hosting doesn't add IHttpContextAccessor by default services.TryAddSingleton(); // Identity services services.TryAddScoped, UserValidator>(); services.TryAddScoped, PasswordValidator>(); services.TryAddScoped, PasswordHasher>(); services.TryAddScoped(); // No interface for the error describer so we can add errors without rev'ing the interface services.TryAddScoped(); services.TryAddScoped>(); services.TryAddScoped, UserClaimsPrincipalFactory>(); services.TryAddScoped, AspNetUserManager>(); services.TryAddScoped, SignInManager>(); if (setupAction != null) { services.Configure(setupAction); } return new IdentityBuilder(typeof(TUser), services); } } }