возможно ли получить пользовательский экземпляр UserManager внутри "Configure" (IConfigureOptions)? Когда я пытаюсь получить экземпляр внутри «Configure», я получаю следующую ошибку внутри ApplicationUserManager class. Спасибо!
System.InvalidOperationException: 'ValueFactory attempted to access the Value property of this instance.'
Мой код
ConfigureIdentityOptions
public class ConfigureIdentityOptions : IConfigureOptions<IdentityOptions>
{
private readonly IServiceScopeFactory _serviceScopeFactory;
public ConfigureIdentityOptions(IServiceScopeFactory serviceScopeFactory)
{
_serviceScopeFactory = serviceScopeFactory;
}
public void Configure(IdentityOptions identityOptions)
{
using (var currentScope = _serviceScopeFactory.CreateScope())
{
using (var userManager = currentScope.ServiceProvider.GetRequiredService<ApplicationUserManager>())
{
}
}
}
}
ConfigureServices
public void ConfigureServices(IServiceCollection serviceCollection)
{
serviceCollection.AddDbContext<DatabaseContext>(x =>
x.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
serviceCollection.ConfigureOptions<ConfigureIdentityOptions>();
serviceCollection.AddIdentity<ApplicationUser, ApplicationRole>(x =>
{
...
})
.AddEntityFrameworkStores<DatabaseContext>()
.AddRoleStore<ApplicationRoleStore>()
.AddUserStore<ApplicationUserStore>()
.AddUserManager<ApplicationUserManager>()
.AddRoleManager<ApplicationRoleManager>()
.AddSignInManager<ApplicationSignInManager>()
.AddDefaultTokenProviders();
}
ApplicationUserManager
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> userStore, IOptions<IdentityOptions> optionsAccessor,
IPasswordHasher<ApplicationUser> passwordHasher,
IEnumerable<IUserValidator<ApplicationUser>> userValidators,
IEnumerable<IPasswordValidator<ApplicationUser>> passwordValidators, ILookupNormalizer keyNormalizer,
IdentityErrorDescriber identityErrorDescriber, IServiceProvider serviceProvider,
ILogger<UserManager<ApplicationUser>> logger) :
base(userStore, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer,
identityErrorDescriber,
serviceProvider, logger)
{ }
}