这个错误是在第一次尝试添加迁移时出现的。我已经添加了扩展方法和我的服务类。
公共 void ConfigureServices(IServiceCollection服务) {
公共类CustomClaimPrincipalFactory:UserClaimsPrincipalFactory<ApplicationUser,IdentityRole> { }
公共 void ConfigureServices(IServiceCollection服务) {
services.AddCors();
services.AddControllers();
services.Configure<AppSettings>(AppSettings);
services.ConfigureJWT(Configuration);
services.ConfigureIdentity();
services.AddScoped<ILoginService, LoginService>();
services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, CustomClaimPrincipalFactory>();
}
公共静态方法 ConfigureIdentity(this IServiceCollection services),用于配置身份验证。
var builder = services.AddIdentityCore<ApplicationUser>(o =>
{
o.Password.RequireDigit = true;
o.Password.RequireLowercase = false;
o.Password.RequireUppercase = false;
o.Password.RequireNonAlphanumeric = false;
o.Password.RequiredLength = 6;
o.User.RequireUniqueEmail = true;
});
builder = new IdentityBuilder(builder.UserType, typeof(IdentityRole),
builder.Services);
builder.AddEntityFrameworkStores<AuthDBContext>()
.AddDefaultTokenProviders();
}
公共类CustomClaimPrincipalFactory:UserClaimsPrincipalFactory<ApplicationUser,IdentityRole> { }
public CustomClaimPrincipalFactory(
UserManager<ApplicationUser> userManager,
RoleManager<IdentityRole> roleManager,
IOptions<IdentityOptions> optionsAccessor)
: base(userManager, roleManager, optionsAccessor)
{
}
protected override async Task<ClaimsIdentity> GenerateClaimsAsync(ApplicationUser user)
{
ClaimsIdentity identity = await base.GenerateClaimsAsync(user);
identity.AddClaim(new Claim("ServiceSite", user.ServiceSite ?? ""));
return identity;
}
}
这里又出现了错误
访问 Microsoft.Extensions.Hosting 服务时发生错误。继续不使用应用程序服务提供程序。错误:某些服务无法构建(验证服务描述符时出错'ServiceType: Microsoft.AspNetCore.Identity.IUserClaimsPrincipalFactory1[AuthenticationService.Entities.ApplicationUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserClaimsPrincipalFactory
1[AuthenticationService.Entities.ApplicationUser]':在激活'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore9[AuthenticationService.Entities.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,AuthenticationService.Data.AuthDBContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim
1[System.String]]'时无法解析类型为'AuthenticationService.Data.AuthDBContext'。)(验证服务描述符时出错'ServiceType: Microsoft.AspNetCore.Identity.UserManager1[AuthenticationService.Entities.ApplicationUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserManager
1[AuthenticationService.Entities.ApplicationUser]':在激活'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore9[AuthenticationService.Entities.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,AuthenticationService.Data.AuthDBContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim
1[System.String]]'时无法解析类型为'AuthenticationService.Data.AuthDBContext'。)(验证服务描述符时出错'ServiceType: Microsoft.AspNetCore.Identity.IUserStore1[AuthenticationService.Entities.ApplicationUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore
9[AuthenticationService.Entities.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,AuthenticationService.Data.AuthDBContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken
1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim1[System.String]]':在激活'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore
9[AuthenticationService.Entities.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,AuthenticationService.Data.AuthDBContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken
1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim1[System.String]]'时无法解析类型为'AuthenticationService.Data.AuthDBContext'。)(验证服务描述符时出错'ServiceType: Microsoft.AspNetCore.Identity.IRoleStore
1[Microsoft.AspNetCore.Identity.IdentityRole] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.EntityFrameworkCore.RoleStore5[Microsoft.AspNetCore.Identity.IdentityRole,AuthenticationService.Data.AuthDBContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserRole
1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim1[System.String]]':在激活'Microsoft.AspNetCore.Identity.EntityFrameworkCore.RoleStore
5[Microsoft.AspNetCore.Identity.IdentityRole,AuthenticationService.Data.AuthDBContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserRole1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim
1[System.String]]'时无法解析类型为'AuthenticationService.Data.AuthDBContext'。)(验证服务描述符时出错'ServiceType: Microsoft.AspNetCore.Identity.IUserClaimsPrincipalFactory1[AuthenticationService.Entities.ApplicationUser] Lifetime: Scoped ImplementationType: AuthenticationService.Common.CustomClaimPrincipalFactory':在激活'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore
9[AuthenticationService.Entities.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,AuthenticationService.Data.AuthDBContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken
1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim`1[System.String]]'时无法解析类型为'AuthenticationService.Data.AuthDBContext'。)