asp.net - Asp.Net Core 3 Web API 身份
问题描述
我已经存货 3 天了,没有可能的在线解决方案出现此错误
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.SecurityStampValidator`1[MyTrBox_WebAPI.Model.User]': Unable to resolve service for type 'Microsoft.AspNetCore.Authentication.ISystemClock' while attempting to activate 'Microsoft.AspNetCore.Identity.SecurityStampValidator`1[MyTrBox_WebAPI.Model.User]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.ITwoFactorSecurityStampValidator Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.TwoFactorSecurityStampValidator`1[MyTrBox_WebAPI.Model.User]': Unable to resolve service for type 'Microsoft.AspNetCore.Authentication.ISystemClock' while attempting to activate 'Microsoft.AspNetCore.Identity.TwoFactorSecurityStampValidator`1[MyTrBox_WebAPI.Model.User]'.)
Source=Microsoft.Extensions.DependencyInjection
StackTrace:
at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable`1 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`1.CreateServiceProvider(Object containerBuilder)
at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider()
at Microsoft.Extensions.Hosting.HostBuilder.Build()
at MyTrBox_WebAPI.Program.Main(String[] args) in C:\Users\Biosec-PC-Samuel\Source\Repos\MyTrBox WebAPI\MyTrBox WebAPI\Program.cs:line 16
Inner Exception 1:
InvalidOperationException: Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.ISecurityStampValidator Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.SecurityStampValidator`1[MyTrBox_WebAPI.Model.User]': Unable to resolve service for type 'Microsoft.AspNetCore.Authentication.ISystemClock' while attempting to activate 'Microsoft.AspNetCore.Identity.SecurityStampValidator`1[MyTrBox_WebAPI.Model.User]'.
Inner Exception 2:
InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Authentication.ISystemClock' while attempting to activate 'Microsoft.AspNetCore.Identity.SecurityStampValidator`1[MyTrBox_WebAPI.Model.User]'.
这是我在启动时用来添加身份的代码
var builder = services.AddIdentityCore<User>();
builder = new IdentityBuilder(
builder.UserType,
typeof(UserRole),
builder.Services);
builder.AddRoles<UserRole>()
.AddEntityFrameworkStores<AppDbContext>()
.AddDefaultTokenProviders()
.AddSignInManager<SignInManager<User>>();
用户等级:
public class User : IdentityUser<Guid>
{
public string Country { get; set; }
public DateTimeOffset CreatedAt { get; set; }
}
用户角色类:
public class UserRole : IdentityRole<Guid>
{
public UserRole()
: base() {
}
public UserRole(string roleName)
: base(roleName) {
}
}
当我对 API 服务器进行任何调用时,我总是遇到上述错误提前谢谢
编辑:
.csproj:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<RootNamespace>MyTrBox_WebAPI</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoMapper" Version="9.0.0" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
</ItemGroup>
</Project>
解决方案
对于自定义 asp.net 核心身份,您可以按照以下步骤操作:
.csproj
<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> <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.0.0" /> <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.0.0" /> </ItemGroup> </Project>
CoreSecurityContext
public class CoreSecurityContext : IdentityDbContext<User, UserRole, Guid> { public CoreSecurityContext(DbContextOptions<CoreSecurityContext> 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.AddDefaultIdentity<User>() .AddRoles<UserRole>() .AddEntityFrameworkStores<CoreSecurityContext>();
推荐阅读
- javascript - 访问对象拥有数组内的键值
- jquery - 按 ID 选中多个复选框
- linux-device-driver - 如何在不重新启动系统的情况下将服务 USB 重置为 lsmod 中列出的串行驱动程序?
- javascript - 将可变数量的过滤条件应用于javascript数组,同时只遍历一次?
- amazon-web-services - CloudWatch 监控和通知
- node.js - 尝试运行“npm run dev”但它不起作用
- c# - Bot Emulator Framework 无法解析服务
- excel - 将首字母大写文本和首字母小写文本复制到两列
- spring - 如果 Spring boot REST Api 中存在额外参数,如何拒绝请求并发送自定义消息
- javascript - 列表 ul firebase 的错误顺序