我正在尝试使用 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 应用程序,并具有身份验证 = 个人用户帐户。
那么我需要做更多的事情才能使我的应用程序首次运行无异常呢?
我尝试了互联网上的很多例子,但我发现的所有例子都已经过时了。