我正在尝试使用ASP.NET Core 1(2016年5月)构建新网站,并且我需要实现其他类型的登录过程(不适用于SQL Server)。
因此,我正在尝试实现MyOwnUserStore
,在这里我想覆盖登录过程,但是当我启动应用程序时,结果是此异常:
InvalidOperationException:尝试激活“ LoginWebApp04.Services.MyOwnUserStore”时,无法解析类型为“ Microsoft.EntityFrameworkCore.DbContext”的服务。
我的代码很简单,新类:
public class MyOwnUserStore : UserStore<ApplicationUser>
{
public MyOwnUserStore(DbContext context, IdentityErrorDescriber describer = null)
: base(context, describer)
{
}
...
}
和修改Startup.cs
:
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
var idt = services.AddIdentity<ApplicationUser, IdentityRole>();
idt.AddUserStore<MyOwnUserStore>(); // <-------------------------- HERE
idt.AddEntityFrameworkStores<ApplicationDbContext>();
idt.AddDefaultTokenProviders();
services.AddMvc();
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
}
在我的新项目中,我没有做任何其他更改,并且该项目被创建为ASP.NET Core Web Application,身份验证=个人用户帐户。
那么,我需要做更多的工作才能无例外地首次运行我的应用程序吗?
我已经尝试了许多来自互联网的示例,但是发现的都已经过时了。
DI框架不知道要为用户存储解决什么。DbContext
在构造函数中更改为ApplicationDbContext
public class MyOwnUserStore : UserStore<ApplicationUser> {
public MyOwnUserStore(ApplicationDbContext context, IdentityErrorDescriber describer = null)
: base(context, describer) {
}
...
}
更改添加用户存储的顺序。您需要先添加存储的实体框架,然后再添加用户存储。
public void ConfigureServices(IServiceCollection services) {
// Add framework services.
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services
.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddUserStore<MyOwnUserStore>() // <----MOVED AFTER ADDING EF STORES
.AddDefaultTokenProviders();
services.AddMvc();
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句