c# - AspNet Core3 身份配置

标签 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 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

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]'.

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]'.



如果我实现
public class MyUserStore : IUserStore<IdentityUser>
并添加一行
services.AddScoped<IUserStore<IdentityUser>, MyUserStore>();
到 ConfigurationServices,我收到关于下一个错误

Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IRoleStore



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

最佳答案

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

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

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

关于c# - AspNet Core3 身份配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57991925/

相关文章:

c# - 从网页发送文档到打印机

c# - 从命令行运行单元测试

c# - 如何在使用 DapperExtensions 映射时指定 Schema?

c# - 在模型和 AspNetUsers 之间加入(包含)

c# - 在 GridView 中翻转项目图像

c# - html 敏捷包对象引用未设置为对象的实例

c# - 为什么 EF Core 更新无法更新已修改的列

asp.net-core - 如何在 ASP.NET Core 中获取中间件列表

.net - 使用 EFCore 和 Identity for Core

.net - ASP.NET MVC 尝试加载旧版本的 Owin 程序集