Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. public ServiceFactory()
  2. {
  3. var services = new ServiceCollection();
  4. services.AddDbContext<ApplicationDbContext>
  5. (
  6. options => options.UseInMemoryDatabase(Guid.NewGuid().ToString())
  7.  
  8. );
  9.  
  10.  
  11. IdentityBuilder builder = services.AddIdentityCore<User>(options =>
  12. {
  13. options.Password.RequireDigit = false;
  14. options.Password.RequireUppercase = false;
  15. options.Password.RequiredLength = 8;
  16. options.Password.RequireDigit = false;
  17. options.Password.RequireLowercase = false;
  18. options.Password.RequireNonAlphanumeric = false;
  19. });
  20. builder = new IdentityBuilder(builder.UserType, typeof(Role), builder.Services);
  21.  
  22. builder.AddEntityFrameworkStores<ApplicationDbContext>();
  23. builder.AddRoleValidator<RoleValidator<Role>>();
  24. builder.AddRoleManager<RoleManager<Role>>();
  25. builder.AddSignInManager<SignInManager<User>>();
  26. // Taken from https://github.com/aspnet/MusicStore/blob/dev/test/MusicStore.Test/ManageControllerTest.cs (and modified)
  27. // IHttpContextAccessor is required for SignInManager, and UserManager
  28. var context = new DefaultHttpContext();
  29. context.Features.Set<IHttpAuthenticationFeature>(new HttpAuthenticationFeature());
  30. services.AddSingleton<IHttpContextAccessor>(h => new HttpContextAccessor { HttpContext = context });
  31. var serviceProvider = services.BuildServiceProvider();
  32. this.Context = serviceProvider.GetRequiredService<ApplicationDbContext>();
  33. this.UserManager = serviceProvider.GetRequiredService<UserManager<User>>();
  34.  
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement