我正在使用ASP.NET Core MVC 3.0 API项目。我正在使用Microsoft.AspNetCore.Identity
v3.0
在Startup.cs
中,在ConfigureServices
方法中,当我键入以下内容并运行应用程序时,我会得到一个异常:
var builder = services.AddIdentityCore<AppUser>();
var identityBuilder = new IdentityBuilder(builder.UserType, builder.Services);
identityBuilder.AddEntityFrameworkStores<DataContext>();
identityBuilder.AddSignInManager<SignInManager<AppUser>>();
AppUser
类是这样的:
using Microsoft.AspNetCore.Identity;
namespace Domain
{
public class AppUser : IdentityUser
{
public string DisplayName { get; set; }
}
}
我得到的例外是:
未处理的异常。System.AggregateException:无法构造某些服务(验证服务描述符"ServiceType:Microsoft.AspNetCore.Identity.ISecurityStampValidator Lifetime:Scoped ImplementationType:Microsoft.AspNetCore.Identity.SecurityStampValidator`1[Domain.AppUser]时出错":在尝试激活"Microsoft.AspNetCore.Identity.SecurityStampValidator"1[Domain.AppUser]"时,无法解析类型"Microsoft.AspNetCore.Authentication.ISystemClock"的服务。)"(验证服务描述符"ServiceType:Microsoft.AspNetCore.Identity.ITwoFactorSecurityStampValidator Lifetime:Scoped ImplementationType:Microsoft.AspNetCore.Identity.TwoFactorSecurityStamp Validator `1[Domain.AppUser]时出错":在尝试激活"Microsoft.AspNetCore.Identity.TwoFactorSecurityStampValidator"1[Domain.AppUser]"时,无法解析类型"Microsoft.AspNetCore.Authentication.ISystemClock"的服务。)">
System.InvalidOperationException:验证服务描述符"ServiceType:Microsoft.AspNetCore.Identity.ISecurityStampValidator Lifetime:Scoped ImplementationType:Microsoft.AspNetCore.Identity.SecurityStampValidator `1[Domain.AppUser]"时出错:尝试激活时无法解析类型为"Microsoft.AspNet Core.Authentication.ISystemClock"的服务'Microsoft.AspNetCore.Identity.SecurityStampValidator `1[Domain.AppUser]'.
System.InvalidOperationException:在尝试激活"Microsoft.AspNetCore.Identity.SecurityStampValidator `1[Domain.AppUser]"时,无法解析类型为"Microsoft.AspNetCore.Authentication.ISystemClock"的服务。
在Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(类型serviceType,类型implementationType,CallSiteChain CallSiteChain,ParameterInfo[]参数,布尔throwIfCallSiteNotFound)
上(ResultCache生存期,类型serviceType,类型implementationType,CallSiteChain CallSiteChain)
,位于Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.GetCallSite(ServiceDescriptor ServiceDescriptor,CallSiteChain CallSiteChain)
位于Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.ValideService(ServiceDescriptorDescriptor描述符)
---内部异常堆栈跟踪结束---
处Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable`1 serviceDescriptors,ServiceProviderOptions选项)
---内部异常堆栈跟踪结束---
在Microsoft.Extensions.DdependencyInraction.ServiceProvider.ctor(IEnumarable`1 servicesDescriptors,ServiceProviderOptions选项位于Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection服务,ServiceProviderOptions选项)位于Microsoft.Extensions.DependencyInjection.DefaultServiceProviderFactory.CreateServiceProvider(IServiceCollection containerBuilder)位于Microsoft.Extensions.Hosting.Internal.ServiceFactoryAdapter`1.CreateServiceProvider(对象容器生成器)位于Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider()位于Microsoft.Extensions.Hosting.HostBuilder.Build()位于C:\Projects\XXXXXXXX\Api\Program.cs:line 19中的Api.Program.Main(String[]args)--->(内部异常#1)System.InvalidOperationException:验证服务描述符"ServiceType:Microsoft.AspNetCore.Identity.ITwoFactorSecurityStampValidator生存期:Scoped"时出错实现类型:Microsoft.AspNetCore.Identity.TwoFactorSecurityStampValidator"1[Domain.AppUser]":在尝试激活"Microsoft.AspNetCore.Identity.TwoFactorSecurityStampValidator `1[Domain.AppUser]]"时,无法解析类型为"Microsoft.AspNetCore.Authentication.ISystemClock"的服务。--->System.InvalidOperationException:在尝试激活"Microsoft.AspNetCore.Identity.TwoFactorSecurityStampValidator `1[Domain.AppUser]"时,无法解析类型为"Microsoft.AspNetCore.Authentication.ISystemClock"的服务。位于Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(类型serviceType,类型implementationType,CallSiteChain CallSiteChain,ParameterInfo[]parameters,布尔throwIfCallSiteNotFound)在Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache生存期,类型serviceType,类型implementationType,CallSiteChain CallSiteChain位于Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.GetCallSite(ServiceDescriptor ServiceDescriptor,CallSiteChain CallSiteChain)位于Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.ValideService(ServiceDescriptor描述符)---内部异常堆栈跟踪结束---位于Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.ValideService(ServiceDescriptor描述符)位于Microsoft.Extensions.DependencyInjection.ServiceProvider.ctor(IEnumerable`1 serviceDescriptors,ServiceProviderOptions选项)<---
知道我做错了什么吗?
最好使用AddDefaultIdentity
,它可以帮助您注册像SignInManager
这样的服务。您可以参考以下步骤:
-
编辑项目文件以安装包:
<Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>netcoreapp3.0</TargetFramework> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.0.0" /> <PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.0.0" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.0.0"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> </PackageReference> </ItemGroup> </Project>
-
创建
DataContext
:public class DataContext : IdentityDbContext<AppUser> { public DataContext(DbContextOptions<DataContext> options) : base(options) { } protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); // Customize the ASP.NET Identity model and override the defaults if needed. // For example, you can rename the ASP.NET Identity table names and more. // Add your customizations after calling base.OnModelCreating(builder); } }
-
Startup.cs:
services.AddDbContext<DataContext>(options => options.UseSqlServer( Configuration.GetConnectionString("DefaultConnection"))); services.AddDefaultIdentity<AppUser>() .AddEntityFrameworkStores<DataContext>();
-
注入
SignInManager
:private readonly UserManager<AppUser> _userManager; private readonly SignInManager<AppUser> _signInManager; private readonly DataContext _context; public WeatherForecastController(UserManager<AppUser> userManager,SignInManager<AppUser> signInManager, DataContext dbContext) { _userManager = userManager; _signInManager = signInManager; _context = dbContext; }
并使用类似:
await _signInManager.SignInAsync(user, isPersistent: false);
要解决此问题,请在Startup.cs
中的ConfigurationServices
方法内,在以下代码块后添加services.AddAuthentication();
:
var builder = services.AddIdentityCore<AppUser>();
var identityBuilder = new IdentityBuilder(builder.UserType, builder.Services);
identityBuilder.AddEntityFrameworkStores<DataContext>();
identityBuilder.AddSignInManager<SignInManager<AppUser>>();
对于任何跟随Neil Cummings课程使用.Net Core和React构建应用程序的完整指南的人,这是他稍后提供的解决方案。