using System; using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Bit.Core.Models.Table; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; namespace Bit.Core.Identity { public static class JwtBearerIdentityServiceCollectionExtensions { public static IdentityBuilder AddJwtBearerIdentity( this IServiceCollection services) { return services.AddJwtBearerIdentity(setupAction: null, jwtBearerSetupAction: null); } public static IdentityBuilder AddJwtBearerIdentity( this IServiceCollection services, Action setupAction, Action jwtBearerSetupAction) { // Services used by identity services.AddOptions(); services.AddAuthentication(); // Hosting doesn't add IHttpContextAccessor by default services.TryAddSingleton(); // Identity services services.TryAddSingleton(); services.TryAddScoped, UserValidator>(); services.TryAddScoped, PasswordValidator>(); services.TryAddScoped, PasswordHasher>(); services.TryAddScoped(); services.TryAddScoped, RoleValidator>(); // 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, UserManager>(); services.TryAddScoped(); services.TryAddScoped, RoleManager>(); if(setupAction != null) { services.Configure(setupAction); } if(jwtBearerSetupAction != null) { services.Configure(jwtBearerSetupAction); } return new IdentityBuilder(typeof(User), typeof(Role), services); } } }