AspNet Core3身份配置 [英] AspNet Core3 Identity configuration

查看:63
本文介绍了AspNet Core3身份配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

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

I already have a database with a couple of tables created by EntityFrameworkCore3.00-preview, it works fine.

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

Now I'm trying to add autorization into project So I derive my database context class from IdentityDbContext,

公共类MyDbContext:IdentityDbContext

代替

公共类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>();

到Startup.cs中的ConfigurationServices

to ConfigurationServices in Startup.cs

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

application crashes with message:

System.AggregateExceptionHResult = 0x80131500消息=无法构造某些服务(验证服务描述符'ServiceType:Microsoft.AspNetCore.Identity.ISecurityStampValidator生存期:范围实现类型:Microsoft.AspNetCore.Identity.SecurityStampValidator 1 [Microsoft.AspNetCore.Identity.IdentityUser]':尝试激活'Microsoft.AspNetCore.Identity.UserManager 1 [时无法解析类型为Microsoft.AspNetCore.Identity.IUserStore 1 [Microsoft.AspNetCore.Identity.IdentityUser]]的服务[Microsoft.AspNetCore.Identity.IdentityUser].)(验证服务描述符'ServiceType:Microsoft.AspNetCore.Identity.ITwoFactorSecurityStampValidator寿命:范围实现类型:Microsoft.AspNetCore.Identity.TwoFactorSecurityStampValidator 1 [Microsoft.AspNetCore.Identity.IdentityUser]':尝试激活时无法解析类型为'Microsoft.AspNetCore.Identity.IUserStore 1 [Microsoft.AspNetCore.Identity.IdentityUser]'的服务e'Microsoft.AspNetCore.Identity.UserManager 1 [Microsoft.AspNetCore.Identity.IdentityUser]'.)(验证服务描述符'ServiceType:Microsoft.AspNetCore.Identity.IUserClaimsPrincipalFactory 1 [Microsoft.AspNetCore.Identity.IdentityUser]寿命:范围内的实现类型:Microsoft.AspNetCore.Identity.UserClaimsPrincipalFactory 2 [Microsoft.AspNetCore.Identity.IdentityUser,Microsoft.AspNetCore.Identity.IdentityRole]':无法解析类型为"Microsoft"的服务.AspNetCore.Identity.IUserStore 1 [Microsoft.AspNetCore.Identity.IdentityUser]",同时尝试激活"Microsoft.AspNetCore.Identity.UserManager 1 [Microsoft.AspNetCore.Identity.IdentityUser]".)(验证服务描述符'ServiceType:Microsoft.AspNetCore.Identity.UserManager 1 [Microsoft.AspNetCore.Identity.IdentityUser]生命周期:范围实现类型:Microsoft.AspNetCore.Identity.UserManager 1 [Microsoft.AspNetCore.Identity.IdentityUser]":尝试激活"Microsoft.AspNetCore.Identity.IdentityUser] 1 [Microsoft]时,无法解析类型为"Microsoft.AspNetCore.Identity.IUserStore 1 [Microsoft.AspNetCore.Identity.IdentityUser]"的服务.AspNetCore.Identity.IdentityUser].)(验证服务描述符'ServiceType:Microsoft.AspNetCore.Identity.SignInManager 1 [Microsoft.AspNetCore.Identity.IdentityUser]时出错:范围内的实现类型:Microsoft.AspNetCore.Identity.SignInManager 1 [Microsoft.AspNetCore.Identity.IdentityUser]':在尝试激活时无法解析类型为"Microsoft.AspNetCore.Identity.IUserStore 1 [Microsoft.AspNetCore.Identity.IdentityUser]"的服务'Microsoft.AspNetCore.Identity.UserManager 1 [Microsoft.AspNetCore.Identity.IdentityUser]'.)(验证服务描述符'ServiceType:Microsoft.AspNetCore.Identity.RoleManager 1 [Microsoft.AspNetCore时出错.Identity.IdentityRole]生存期:范围内的实现类型:Microsoft.AspNetCore.Identity.RoleManager 1 [Microsoft.AspNetCore.Identity.IdentityRole]':无法解析类型'Microsoft.AspNetCore.Identity.IRoleStore 1 [Microsoft.AspNetCore.Identity.IdentityRole]的服务",同时尝试激活"Microsoft.AspNetCore.Identity.RoleManager 1 [Microsoft.AspNetCore.Identity.IdentityRole]".)源= Microsoft.Extensions.DependencyInjection堆栈跟踪:在Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable 1 serviceDescriptors,ServiceProviderOptions选项)在Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection服务,ServiceProviderOptions选项)在Microsoft.Extensions.DependencyInjection.DefaultServiceProviderFactory.CreateServiceProvider(IServiceCollection containerBuilder)在Microsoft.Extensions.Hosting.Internal.ServiceFactoryAdapter 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)中

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.SecurityStampValidator1[Microsoft.AspNetCore.Identity.IdentityUser]': Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IUserStore1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager1[Microsoft.AspNetCore.Identity.IdentityUser]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.ITwoFactorSecurityStampValidator Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.TwoFactorSecurityStampValidator1[Microsoft.AspNetCore.Identity.IdentityUser]': Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IUserStore1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager1[Microsoft.AspNetCore.Identity.IdentityUser]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.IUserClaimsPrincipalFactory1[Microsoft.AspNetCore.Identity.IdentityUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserClaimsPrincipalFactory2[Microsoft.AspNetCore.Identity.IdentityUser,Microsoft.AspNetCore.Identity.IdentityRole]': Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IUserStore1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager1[Microsoft.AspNetCore.Identity.IdentityUser]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.UserManager1[Microsoft.AspNetCore.Identity.IdentityUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserManager1[Microsoft.AspNetCore.Identity.IdentityUser]': Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IUserStore1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager1[Microsoft.AspNetCore.Identity.IdentityUser]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.SignInManager1[Microsoft.AspNetCore.Identity.IdentityUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.SignInManager1[Microsoft.AspNetCore.Identity.IdentityUser]': Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IUserStore1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager1[Microsoft.AspNetCore.Identity.IdentityUser]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.RoleManager1[Microsoft.AspNetCore.Identity.IdentityRole] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.RoleManager1[Microsoft.AspNetCore.Identity.IdentityRole]': Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IRoleStore1[Microsoft.AspNetCore.Identity.IdentityRole]' while attempting to activate 'Microsoft.AspNetCore.Identity.RoleManager1[Microsoft.AspNetCore.Identity.IdentityRole]'.) Source=Microsoft.Extensions.DependencyInjection StackTrace: at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable1 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.ServiceFactoryAdapter1.CreateServiceProvider(Object containerBuilder) at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider() at Microsoft.Extensions.Hosting.HostBuilder.Build() at TheWorld.Program.Main(String[] args) in C:\Users\mli2805\source\repos\TheWorld\TheWorld\Program.cs:line 10

内部异常1:InvalidOperationException:验证服务描述符"ServiceType:Microsoft.AspNetCore.Identity.ISecurityStampValidator生命周期:范围实现类型:Microsoft.AspNetCore.Identity.SecurityStampValidator 1 [Microsoft.AspNetCore.Identity.IdentityUser]":无法为服务解析服务在尝试激活"Microsoft.AspNetCore.Identity.UserManager" 1 [Microsoft.AspNetCore.Identity.IdentityUser]时键入"Microsoft.AspNetCore.Identity.IUserStore 1 [Microsoft.AspNetCore.Identity.IdentityUser]".

Inner Exception 1: InvalidOperationException: Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.ISecurityStampValidator Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.SecurityStampValidator1[Microsoft.AspNetCore.Identity.IdentityUser]': Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IUserStore1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]'.

内部异常2:InvalidOperationException:在尝试激活"Microsoft.AspNetCore.Identity.UserManager 1 [Microsoft.AspNetCore.Identity.IdentityUser]".

Inner Exception 2: InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IUserStore1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager1[Microsoft.AspNetCore.Identity.IdentityUser]'.

如果我实施公共类MyUserStore:IUserStore< IdentityUser>

并添加一行 services.AddScoped< IUserStore< IdentityUser> ;, MyUserStore>();

到ConfigurationServices,我收到有关

to ConfigurationServices, I receive next error about

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

Unable to resolve service for type '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?

推荐答案

您需要添加 AddEntityFrameworkStores< TContext>():

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

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

FWIW, ASP.NET Core 1.1 is not "a little outdated". Documentation referencing that version is so outdated as to be virtually useless. So much changed in 2.X, and even more in 3.X. You should find a different article/tutorial.

这篇关于AspNet Core3身份配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆