简体   繁体   English

AspNet Core3身份配置

[英]AspNet Core3 Identity configuration

I'm trying to follow a little bit out of date tutorial project for AspNet Core 1.1 (while I'm using Visual Studio 2019 preview 2.0, and .net core v3.0.0-preview9 installed) 我正在尝试遵循AspNet Core 1.1的一些过时教程项目(当我使用Visual Studio 2019预览2.0并安装了.net core v3.0.0-preview9时)

I already have a database with a couple of tables created by EntityFrameworkCore3.00-preview, it works fine. 我已经有一个包含几个由EntityFrameworkCore3.00-preview创建的表的数据库,它工作正常。

Now I'm trying to add autorization into project So I derive my database context class from IdentityDbContext, 现在,我正在尝试将autorization添加到项目中,所以我从IdentityDbContext派生我的数据库上下文类,

public class MyDbContext : IdentityDbContext

instead of 代替

public class WorldContext : DbContext

make add-migration and update-database and in database I see many new tables 进行添加迁移和更新数据库,并在数据库中看到许多新表

At this point my application could start, but at the very moment as I added a line 此时我的应用程序可以启动,但是在我添加一行代码的那一刻

services.AddIdentity<IdentityUser, IdentityRole>();

to ConfigurationServices in Startup.cs 到Startup.cs中的ConfigurationServices

application crashes with message: 应用程序崩溃并显示以下消息:

System.AggregateException HResult=0x80131500 Message=Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.ISecurityStampValidator Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.SecurityStampValidator 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 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]': Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IUserStore 1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager 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尝试激活“ 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]': Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IUserStore 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]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.ITwoFactorSecurityStampValidator Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.TwoFactorSecurityStampValidator 1 [Microsoft.AspNetCore.Identity.IdentityUser]':无法访问1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager解析类型为“ Microsoft.AspNetCore.Identity.IUserStore 1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager 1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager 1[Microsoft.AspNetCore.Identity.IdentityUser]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.IUserClaimsPrincipalFactory 1[Microsoft.AspNetCore.Identity.IdentityUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserClaimsPrincipalFactory 2[Microsoft.AspNetCore.Identity.IdentityUser,Microsoft.AspNetCore.Identity.IdentityRole]': Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IUserStore 1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager 1[Microsoft.AspNetCore.Identity.IdentityUser]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.UserManager 1[Microsoft.AspNetCore.Identity.IdentityUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserManager 1[Microsoft.AspNetCore.Identity.IdentityUser]': Unable to resolve 1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager 1 [Microsoft.AspNetCore.Identity.IdentityUser]”。)(验证服务描述符“ ServiceType:Microsoft.AspNetCore.Identity.IUserClaimsPrincipalFactory 1[Microsoft.AspNetCore.Identity.IdentityUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserClaimsPrincipalFactory 2 [Microsoft.AspNetCore.Identity.IdentityUser,Microsoft.AspNetCore.Identity.IdentityRole]':无法解析类型为“ Microsoft.AspNetCore.Identity.IUserStore 1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager ”的服务1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager 1 [Microsoft.AspNetCore.Identity.IdentityUser]”时。)(验证服务描述符“ ServiceType:Microsoft.AspNetCore.Identity.UserManager”时出错。 1[Microsoft.AspNetCore.Identity.IdentityUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserManager 1 [Microsoft.AspNetCore.Identity.IdentityUser]':无法解析 service for type 'Microsoft.AspNetCore.Identity.IUserStore 1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager 1[Microsoft.AspNetCore.Identity.IdentityUser]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.SignInManager 1[Microsoft.AspNetCore.Identity.IdentityUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.SignInManager 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 1[Microsoft.AspNetCore.Identity.IdentityUser]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.RoleManager 1[Microsoft.AspNetCore.Identity.IdentityRole] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.RoleManager 1[Microsoft.AspNetC 1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager 1 [Microsoft.AspNetCore.Identity.IdentityUser]”时,类型1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager 1 [Microsoft.AspNetCore.Identity.IdentityUser]”的服务。验证服务描述符“ ServiceType:Microsoft.AspNetCore.Identity.SignInManager 1[Microsoft.AspNetCore.Identity.IdentityUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.SignInManager 1 [Microsoft.AspNetCore.Identity.IdentityUser]”:无法1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager 1 [Microsoft.AspNetCore.Identity.IdentityUser]”时,解析类型1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager 1 [Microsoft.AspNetCore.Identity.IdentityUser]”的服务。)(错误验证服务描述符'ServiceType:Microsoft.AspNetCore.Identity.RoleManager 1[Microsoft.AspNetCore.Identity.IdentityRole] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.RoleManager 1 [Microsoft.AspNetC ore.Identity.IdentityRole]': Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IRoleStore 1[Microsoft.AspNetCore.Identity.IdentityRole]' while attempting to activate 'Microsoft.AspNetCore.Identity.RoleManager 1[Microsoft.AspNetCore.Identity.IdentityRole]'.) Source=Microsoft.Extensions.DependencyInjection StackTrace: at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable 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.CreateServiceProvider(Object containerBuilder) at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider() at Microsoft.Extensions.Hosting.HostBuilder.Build() ore.Identity.IdentityRole]': 1[Microsoft.AspNetCore.Identity.IdentityRole]' while attempting to activate 'Microsoft.AspNetCore.Identity.RoleManager 1 [Microsoft.AspNetCore]时,无法解析类型为'Microsoft.AspNetCore.Identity.IRoleStore 1[Microsoft.AspNetCore.Identity.IdentityRole]' while attempting to activate 'Microsoft.AspNetCore.Identity.RoleManager服务.Identity.IdentityRole]'。)源= Microsoft.Extensions.DependencyInjection StackTrace:在Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable 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 .Hosting.HostBuilder.Build() at TheWorld.Program.Main(String[] args) in C:\\Users\\mli2805\\source\\repos\\TheWorld\\TheWorld\\Program.cs:line 10 在C:\\ Users \\ mli2805 \\ source \\ repos \\ TheWorld \\ TheWorld \\ Program.cs:line 10中的TheWorld.Program.Main(String [] args)

Inner Exception 1: InvalidOperationException: Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.ISecurityStampValidator Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.SecurityStampValidator 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`1[Microsoft.AspNetCore.Identity.IdentityUser]'. 内部异常1:InvalidOperationException:验证服务描述符'ServiceType:Microsoft.AspNetCore.Identity.ISecurityStampValidator生存期:范围化的实现类型:Microsoft.AspNetCore.Identity.SecurityStampValidator 1[Microsoft.AspNetCore.Identity.IdentityUser]': Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IUserStore尝试激活“ Microsoft.AspNetCore.Identity.UserManager 1[Microsoft.AspNetCore.Identity.IdentityUser]': Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IUserStore 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]”。

Inner Exception 2: InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IUserStore 1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager 1[Microsoft.AspNetCore.Identity.IdentityUser]'. 内部异常2:InvalidOperationException: 1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager 1 1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager无法解析类型为“ Microsoft.AspNetCore.Identity.IUserStore 1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager ”的服务.IdentityUser]”。

If I implement public class MyUserStore : IUserStore<IdentityUser> 如果我实现public class MyUserStore : IUserStore<IdentityUser>

and add a line services.AddScoped<IUserStore<IdentityUser>, MyUserStore>(); 并添加行services.AddScoped<IUserStore<IdentityUser>, MyUserStore>();

to ConfigurationServices, I receive next error about 到ConfigurationServices,我收到有关以下内容的下一个错误

Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IRoleStore 无法解析类型为'Microsoft.AspNetCore.Identity.IRoleStore的服务

and so on, though in tutorial there is nothing about implementation all those interfaces. 依此类推,尽管在本教程中,所有这些接口的实现都没有。 It's seems to me that I do something wrong in configuration services? 在我看来,我在配置服务中做错了什么?

You need to add AddEntityFrameworkStores<TContext>() : 您需要添加AddEntityFrameworkStores<TContext>()

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

FWIW, ASP.NET Core 1.1 is not "a little outdated". FWIW,ASP.NET Core 1.1并非“有点过时”。 Documentation referencing that version is so outdated as to be virtually useless. 引用该版本的文档已经过时,几乎没有用。 So much changed in 2.X, and even more in 3.X. 2.X发生了很大变化,而3.X发生了很大变化。 You should find a different article/tutorial. 您应该找到其他文章/教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM