IdentityExtension.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using Microsoft.AspNetCore.Identity;
  2. using Microsoft.Extensions.DependencyInjection;
  3. using Microsoft.Extensions.DependencyInjection.Extensions;
  4. using Microsoft.Extensions.Options;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Security.Claims;
  8. using System.Security.Principal;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace MTWorkHR.Identity.Models
  12. {
  13. public static class IdentityExtensions
  14. {
  15. public static IdentityBuilder AddSecondIdentity<TUser, TRole>(
  16. this IServiceCollection services)
  17. where TUser : class
  18. where TRole : class
  19. {
  20. services.TryAddScoped<IUserValidator<TUser>, UserValidator<TUser>>();
  21. services.TryAddScoped<IPasswordValidator<TUser>, PasswordValidator<TUser>>();
  22. services.TryAddScoped<IPasswordHasher<TUser>, PasswordHasher<TUser>>();
  23. services.TryAddScoped<IRoleValidator<TRole>, RoleValidator<TRole>>();
  24. services.TryAddScoped<ISecurityStampValidator, SecurityStampValidator<TUser>>();
  25. services.TryAddScoped<IUserClaimsPrincipalFactory<TUser>, UserClaimsPrincipalFactory<TUser, TRole>>();
  26. services.TryAddScoped<UserManager<TUser>, AspNetUserManager<TUser>>();
  27. services.TryAddScoped<SignInManager<TUser>, SignInManager<TUser>>();
  28. services.TryAddScoped<RoleManager<TRole>, AspNetRoleManager<TRole>>();
  29. services.TryAddScoped<IUserConfirmation<TUser>, DefaultUserConfirmation<TUser>>();
  30. return new IdentityBuilder(typeof(TUser), typeof(TRole), services);
  31. }
  32. }
  33. }