AspNet Core3 Identity configuration



我正在尝试遵循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>();

到启动中的配置服务.cs

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

System.AggregateException HResult=0x80131500 消息=某些服务无法构造(Microsoft在尝试激活"Microsoft.AspNetCore.Identity.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.ISecurityStampValidator 生存期:作用域实现类型:Microsoft.AspNetCore.Identity.SecurityStampValidator1[Microsoft.AspNetCore.Identity.IdentityUser]': Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IUserStore1[Microsoft.AspNetCore.Identity.IdentityUser]"时出错:无法解析类型'Microsoft.AspNetCore.Identity.IUserStore1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager1[Microsoft.AspNetCore.Identity.IdentityUser]'.((验证服务描述符"ServiceType: Microsoft.AspNetCore.Identity.IUserClaimsPrincipalFactory1[Microsoft.AspNetCore.Identity.IdentityUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserClaimsPrincipalFactory2[Microsoft.AspNetCore.Identity.IdentityUser,Microsoft.AspNetCore.Identity.IdentityRole]"时出错:无法解析类型"Microsoft.AspNetCore.Identity.IUserStore1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager1[Microsoft.AspNetCore.Identity.IdentityUser]"的服务。(验证服务描述符"ServiceType: Microsoft.AspNetCore.Identity.UserManager1[Microsoft.AspNetCore.Identity.IdentityUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserManager1[Microsoft.AspNetCore.Identity.IdentityUser]"时出错:无法解析类型"Microsoft.AspNetCore.Identity.IUserStore1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager1[Microsoft.AspNetCore.Identity.IdentityUser]"的服务。(验证服务描述符"ServiceType: Microsoft.AspNetCore.Identity.SignInManager1[Microsoft.AspNetCore.Identity.IdentityUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.SignInManager1[Microsoft.AspNetCore.Identity.IdentityUser]"时出错:无法解析类型"Microsoft.AspNetCore.Identity.IUserStore1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager1[Microsoft.AspNetCore.Identity.IdentityUser]"的服务。(验证服务描述符"ServiceType: Microsoft.AspNetCore.Identity.RoleManager1[Microsoft.AspNetCore.Identity.IdentityRole] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.RoleManager1[Microsoft.AspNetCore.Identity.IdentityRole]"时出错:无法解析类型"Microsoft.AspNetCore.Identity.IRoleStore1[Microsoft.AspNetCore.Identity.IdentityRole]' while attempting to activate 'Microsoft.AspNetCore.Identity.RoleManager1[Microsoft.AspNetCore.Identity.IdentityRole]"的服务。 Source=Microsoft.Extensions.DependencyInjection 堆栈跟踪: 在 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:在尝试激活"Microsoft.AspNetCore.Identity.UserManager'1[Microsoft.AspNetCore Microsoft1[Microsoft.AspNetCore.Identity.IdentityUser]': Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IUserStoreMicrosoft.IdentityUser]"时验证服务描述符"Service Microsoft Type: .AspNetCore.Identity.Identity.IdentityUser]"时出错。

内部异常 2: InvalidOperationException: 无法解析类型'Microsoft.AspNetCore.Identity.IUserStore1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager1[Microsoft.AspNetCore.Identity.IdentityUser]'的服务。

如果我实施public class MyUserStore : IUserStore<IdentityUser>

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

到配置服务,我收到下一个错误

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

等等,尽管在教程中没有关于实现所有这些接口的内容。 在我看来,我在配置服务中做错了什么?

您需要添加AddEntityFrameworkStores<TContext>()

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

FWIW,ASP.NET Core 1.1并没有"有点过时"。引用该版本的文档已经过时,几乎毫无用处。在 2.X 中发生了如此多的变化,在 3.X 中甚至更多。您应该找到不同的文章/教程。

相关内容

  • 没有找到相关文章

最新更新