LANCommander/LANCommander.Server.Services/Factories/IdentityContextFactory.cs

94 lines
3.5 KiB
C#
Raw Permalink Normal View History

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<User, Role, DatabaseContext, Guid, UserClaim, UserRole, UserLogin, UserToken, RoleClaim>
{
public CustomUserStore(DatabaseContext context) : base(context)
{
}
}
public class CustomRoleStore : RoleStore<Role, DatabaseContext, Guid, UserRole, RoleClaim>
{
public CustomRoleStore(DatabaseContext context) : base(context)
{
}
}
public class IdentityContext : IDisposable
{
2024-11-08 00:38:47 -06:00
public readonly DatabaseContext DatabaseContext;
public readonly UserManager<User> UserManager;
public readonly RoleManager<Role> RoleManager;
public IdentityContext(DatabaseContext databaseContext, IServiceProvider serviceProvider)
{
DatabaseContext = databaseContext;
var userStore = new CustomUserStore(DatabaseContext);
UserManager = new UserManager<User>(
userStore,
serviceProvider.GetRequiredService<IOptions<IdentityOptions>>(),
serviceProvider.GetRequiredService<IPasswordHasher<User>>(),
serviceProvider.GetRequiredService<IEnumerable<IUserValidator<User>>>(),
serviceProvider.GetRequiredService<IEnumerable<IPasswordValidator<User>>>(),
serviceProvider.GetRequiredService<ILookupNormalizer>(),
serviceProvider.GetRequiredService<IdentityErrorDescriber>(),
serviceProvider.GetRequiredService<IServiceProvider>(),
serviceProvider.GetRequiredService<ILogger<UserManager<User>>>());
var roleStore = new CustomRoleStore(DatabaseContext);
RoleManager = new RoleManager<Role>(
roleStore,
serviceProvider.GetRequiredService<IEnumerable<IRoleValidator<Role>>>(),
serviceProvider.GetRequiredService<ILookupNormalizer>(),
serviceProvider.GetRequiredService<IdentityErrorDescriber>(),
serviceProvider.GetRequiredService<ILogger<RoleManager<Role>>>());
}
public void Dispose()
{
UserManager.Dispose();
RoleManager.Dispose();
DatabaseContext.Dispose();
}
}
public class IdentityContextFactory
{
private readonly IDbContextFactory<DatabaseContext> _dbContextFactory;
private readonly IServiceProvider ServiceProvider;
public IdentityContextFactory(IDbContextFactory<DatabaseContext> dbContextFactory, IServiceProvider serviceProvider)
{
_dbContextFactory = dbContextFactory;
ServiceProvider = serviceProvider;
}
public IdentityContext Create()
{
var context = _dbContextFactory.CreateDbContext();
return new IdentityContext(context, ServiceProvider);
}
public async Task<IdentityContext> CreateAsync()
{
var context = await _dbContextFactory.CreateDbContextAsync();
return new IdentityContext(context, ServiceProvider);
}
}
}