[英]AspNet Core3 Identity configuration
我正在尝试遵循AspNet Core 1.1的一些过时教程项目(当我使用Visual Studio 2019预览2.0并安装了.net core v3.0.0-preview9时)
我已经有一个包含几个由EntityFrameworkCore3.00-preview创建的表的数据库,它工作正常。
现在,我正在尝试将autorization添加到项目中,所以我从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
尝试激活“ 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.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.IUserStore1[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.IUserClaimsPrincipalFactory1[Microsoft.AspNetCore.Identity.IdentityUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserClaimsPrincipalFactory
2 [Microsoft.AspNetCore.Identity.IdentityUser,Microsoft.AspNetCore.Identity.IdentityRole]':无法解析类型为“ Microsoft.AspNetCore.Identity.IUserStore1[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]':无法解析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.SignInManager1[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.RoleManager1[Microsoft.AspNetCore.Identity.IdentityRole] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.RoleManager
1 [Microsoft.AspNetC ore.Identity.IdentityRole]':1[Microsoft.AspNetCore.Identity.IdentityRole]' while attempting to activate 'Microsoft.AspNetCore.Identity.RoleManager
1 [Microsoft.AspNetCore]时,无法解析类型为'Microsoft.AspNetCore.Identity.IRoleStore1[Microsoft.AspNetCore.Identity.IdentityRole]' while attempting to activate 'Microsoft.AspNetCore.Identity.RoleManager
服务.Identity.IdentityRole]'。)源= Microsoft.Extensions.DependencyInjection StackTrace:在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.ServiceFactoryAdapter
.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.IdentityUser]': Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IUserStore
尝试激活“ Microsoft.AspNetCore.Identity.UserManager1[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]”。内部异常2:InvalidOperationException:
1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager
11[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager
无法解析类型为“ Microsoft.AspNetCore.Identity.IUserStore1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager
”的服务.IdentityUser]”。
如果我实现public class MyUserStore : IUserStore<IdentityUser>
并添加行services.AddScoped<IUserStore<IdentityUser>, MyUserStore>();
到ConfigurationServices,我收到有关以下内容的下一个错误
无法解析类型为'Microsoft.AspNetCore.Identity.IRoleStore的服务
依此类推,尽管在本教程中,所有这些接口的实现都没有。 在我看来,我在配置服务中做错了什么?
您需要添加AddEntityFrameworkStores<TContext>()
:
services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<WorldContext>();
FWIW,ASP.NET Core 1.1并非“有点过时”。 引用该版本的文档已经过时,几乎没有用。 2.X发生了很大变化,而3.X发生了很大变化。 您应该找到其他文章/教程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.