gpt4 book ai didi

asp.net-core - 是否可以更改 UserManager 的生命周期?

转载 作者:行者123 更新时间:2023-12-04 04:15:47 28 4
gpt4 key购买 nike

我正在尝试更改 UserManager<T>生命周期从 ScopedTransientConfigureServices(IServiceCollection service)里面但我不知道该怎么做。有可能还是我必须注入(inject) IServiceProvider在每个需要 UserManager<T> 的对象中然后每次我需要使用 UserManager 时创建一个新的范围?

最佳答案

您可以通过以下方式覆盖生命周期范围,或者您可以创建自己的用户管理器:

public void ConfigureServices(IServiceCollection services)
{
var connectionString = Configuration.GetConnectionString("DefaultConnection");

services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(connectionString), ServiceLifetime.Transient);

services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = false)
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();

var userManagerServiceDescriptor =
services.FirstOrDefault(descriptor => descriptor.ServiceType == typeof(UserManager<ApplicationUser>));
services.Remove(userManagerServiceDescriptor);

var userStoreServiceDescriptor =
services.FirstOrDefault(descriptor => descriptor.ServiceType == typeof(IUserStore<ApplicationUser>));
services.Remove(userStoreServiceDescriptor);

services.AddTransient<DbContext, ApplicationDbContext>();
services.AddTransient<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>();
services.AddTransient<UserManager<ApplicationUser>>();
}

关于asp.net-core - 是否可以更改 UserManager<T> 的生命周期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60721511/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com