using LANCommander.Server.Data; using LANCommander.Server.Data.Models; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LANCommander.Server.Services.Factories { public class CustomUserStore : UserStore { public CustomUserStore(DatabaseContext context) : base(context) { } } public class CustomRoleStore : RoleStore { public CustomRoleStore(DatabaseContext context) : base(context) { } } public class IdentityContext : IDisposable { public readonly DatabaseContext DatabaseContext; public readonly UserManager UserManager; public readonly RoleManager RoleManager; public IdentityContext(DatabaseContext databaseContext, IServiceProvider serviceProvider) { DatabaseContext = databaseContext; var userStore = new CustomUserStore(DatabaseContext); UserManager = new UserManager( userStore, serviceProvider.GetRequiredService>(), serviceProvider.GetRequiredService>(), serviceProvider.GetRequiredService>>(), serviceProvider.GetRequiredService>>(), serviceProvider.GetRequiredService(), serviceProvider.GetRequiredService(), serviceProvider.GetRequiredService(), serviceProvider.GetRequiredService>>()); var roleStore = new CustomRoleStore(DatabaseContext); RoleManager = new RoleManager( roleStore, serviceProvider.GetRequiredService>>(), serviceProvider.GetRequiredService(), serviceProvider.GetRequiredService(), serviceProvider.GetRequiredService>>()); } public void Dispose() { UserManager.Dispose(); RoleManager.Dispose(); DatabaseContext.Dispose(); } } public class IdentityContextFactory { private readonly IDbContextFactory _dbContextFactory; private readonly IServiceProvider ServiceProvider; public IdentityContextFactory(IDbContextFactory dbContextFactory, IServiceProvider serviceProvider) { _dbContextFactory = dbContextFactory; ServiceProvider = serviceProvider; } public IdentityContext Create() { var context = _dbContextFactory.CreateDbContext(); return new IdentityContext(context, ServiceProvider); } public async Task CreateAsync() { var context = await _dbContextFactory.CreateDbContextAsync(); return new IdentityContext(context, ServiceProvider); } } }