AspNet Core3身份配置

时间:2019-09-18 11:40:00

标签: c# asp.net-core asp.net-identity

我正在尝试遵循AspNet Core 1.1的一些过时教程项目 (当我使用Visual Studio 2019预览版2.0时,  和.net core v3.0.0-preview9已安装)

我已经有一个包含由EntityFrameworkCore3.00-preview创建的几个表的数据库, 效果很好。

现在我正在尝试将自动处理添加到项目中 因此,我从IdentityDbContext派生我的数据库上下文类,

public class MyDbContext : IdentityDbContext

代替

public class WorldContext : DbContext

进行添加迁移和更新数据库,并在数据库中看到许多新表

这时我的应用程序可以启动,但是在我添加一行的那一刻

services.AddIdentity<IdentityUser, IdentityRole>();

到Startup.cs中的ConfigurationServices

应用程序崩溃并显示以下消息:

  

System.AggregateException     HResult = 0x80131500     消息=无法构建某些服务(验证服务描述符'ServiceType:Microsoft.AspNetCore.Identity.ISecurityStampValidator生存期:范围实现类型:Microsoft.AspNetCore.Identity.SecurityStampValidator 1[Microsoft.AspNetCore.Identity.IdentityUser]': Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IUserStore 1 [Microsoft.AspNetCore。 Identity.IdentityUser]”,尝试激活“ Microsoft.AspNetCore.Identity.UserManager 1[Microsoft.AspNetCore.Identity.IdentityUser]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.ITwoFactorSecurityStampValidator Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.TwoFactorSecurityStampValidator 1 [Microsoft.AspNetCore.Identity.IdentityUser]”:无法解析类型为“ Microsoft.AspNetCore.Identity.IUserStore {{ 1}} 1 [Microsoft.AspNetCore.Identity.IdentityUser]”。)(验证服务描述符“ ServiceType:Microsoft.AspNetCore.Identity.IUserClaimsPrincipalFactory 1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager 2 [Microsoft.AspNetCore.Identity.IdentityUser,Microsoft时出错。 AspNetCore.Identity.IdentityRole]”:无法解析类型为“ Microsoft.AspNetCore.Identity.IUserStore 1[Microsoft.AspNetCore.Identity.IdentityUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserClaimsPrincipalFactory 1 [Microsoft.AspNetCore.Identity.IdentityUser]”的服务。)(在验证服务描述符“ ServiceType: Microsoft.AspNetCore.Identity.Us erManager 1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager 1 [Microsoft.AspNetCore.Identity.IdentityUser]':无法解析类型为'Microsoft.AspNetCore.Identity.IUserStore 1[Microsoft.AspNetCore.Identity.IdentityUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserManager 1 [Microsoft.AspNetCore.Identity.IdentityUser]'的服务。) (验证服务描述符'ServiceType:Microsoft.AspNetCore.Identity.SignInManager 1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager 1 [Microsoft.AspNetCore.Identity.IdentityUser]'时出错:无法解析类型为'Microsoft.AspNetCore.Identity.IUserStore {{ 1}} 1 [Microsoft.AspNetCore.Identity.IdentityUser]”。)(验证服务描述符“ ServiceType:Microsoft.AspNetCore.Identity.RoleManager 1[Microsoft.AspNetCore.Identity.IdentityUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.SignInManager 1 [Microsoft.AspNetCore.Identity.IdentityRole]”时出错:无法解析类型为“ Microsoft.AspNetCore.Identity.IRoleStore 1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager 1 [Microsoft.AspNetCore.Identity.IdentityRole]”的服务。)     源= Microsoft.Extensions.DependencyInjection     堆栈跟踪:      在Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable 1[Microsoft.AspNetCore.Identity.IdentityRole] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.RoleManager 1.CreateServiceProvider(Object containerBuilder)      在Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider()      在Microsoft.Extensions.Hosting.HostBuilder.Build()      在C:\ Users \ mli2805 \ source \ repos \ TheWorld \ TheWorld \ Program.cs:line 10中的TheWorld.Program.Main(String [] args)中

     

内部异常1:   InvalidOperationException:在尝试激活服务描述符“ ServiceType:Microsoft.AspNetCore.Identity.ISecurityStampValidator寿命:范围实现类型:Microsoft.AspNetCore.Identity.SecurityStampValidator 1[Microsoft.AspNetCore.Identity.IdentityRole]' while attempting to activate 'Microsoft.AspNetCore.Identity.RoleManager 1 [Microsoft.AspNetCore.Identity.IdentityUser]”时出错“ Microsoft.AspNetCore.Identity.UserManager`1 [Microsoft.AspNetCore.Identity.IdentityUser]”。

     

内部异常2:   InvalidOperationException:无法解析类型为'Microsoft.AspNetCore.Identity.IUserStore 1 serviceDescriptors, ServiceProviderOptions options) at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options) at Microsoft.Extensions.DependencyInjection.DefaultServiceProviderFactory.CreateServiceProvider(IServiceCollection containerBuilder) at Microsoft.Extensions.Hosting.Internal.ServiceFactoryAdapter 1 [Microsoft.AspNetCore.Identity.IdentityUser]'的服务。

如果我实施  1[Microsoft.AspNetCore.Identity.IdentityUser]': Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IUserStore

并添加一行  1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager

到ConfigurationServices,我收到有关

的下一个错误
  

无法解析类型为'Microsoft.AspNetCore.Identity.IRoleStore'的服务

,依此类推,尽管在本教程中,所有这些接口的实现都没有。 在我看来,我在配置服务中做错了什么?

1 个答案:

答案 0 :(得分:4)

您需要添加AddEntityFrameworkStores<TContext>()

services.AddIdentity<IdentityUser, IdentityRole>()
    .AddEntityFrameworkStores<WorldContext>();

FWIW,ASP.NET Core 1.1并非“有点过时”。引用该版本的文档已经过时,几乎没有用。 2.X发生了很大变化,而3.X发生了很大变化。您应该找到其他文章/教程。