mli*_*mli 3 c# asp.net-identity asp.net-core
我正在尝试遵循 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 消息=无法构建某些服务(验证服务描述符时出错:“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.IUserStore1[Microsoft.AspNetCore .Identity.IdentityUser]'同时尝试激活'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]':无法解析类型'Microsoft.AspNetCore.Identity.IUserStore1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager1[Microsoft.AspNetCore. Identity.IdentityUser]'。)(验证服务描述符“ServiceType:Microsoft.AspNetCore.Identity.IUserClaimsPrincipalFactory”时出错1[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.IUserStore”类型的服务1[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 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\程序.cs:第10行内部异常 1:InvalidOperationException:
1[Microsoft.AspNetCore.Identity.IdentityUser]': Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IUserStore在尝试激活“ ServiceType:Microsoft.AspNetCore.Identity.ISecurityStampValidator Lifetime:Scoped ImplementationType:Microsoft.AspNetCore.Identity.SecurityStampValidator 1[Microsoft.AspNetCore.Identity.IdentityUser]”时出错:Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]'。内部异常 2:InvalidOperationException:无法解析“Microsoft.AspNetCore.Identity.IUserStore
1[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,我收到关于下一个错误
无法解析“Microsoft.AspNetCore.Identity.IRoleStore”类型的服务
等等,虽然在教程中没有关于实现所有这些接口的内容。在我看来,我在配置服务中做错了什么?
Chr*_*att 13
您需要添加AddEntityFrameworkStores<TContext>():
services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<WorldContext>();
Run Code Online (Sandbox Code Playgroud)
FWIW,ASP.NET Core 1.1 并没有“有点过时”。引用该版本的文档已经过时,几乎毫无用处。2.X 中发生了如此大的变化,3.X 中的变化更大。您应该找到不同的文章/教程。
| 归档时间: |
|
| 查看次数: |
9064 次 |
| 最近记录: |