第一次尝试添加迁移时出现此错误。我已经添加了扩展方法和我的服务类
public 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>();
}
public static void ConfigureIdentity(这个 IServiceCollection 服务) {
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
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.IUserClaimsPrincipalFactory
1[AuthenticationService.Entities.ApplicationUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserClaimsPrincipalFactory
1 [AuthenticationService.Entities.ApplicationUser]”时出错:无法解析类型为“AuthenticationService.Data”的服务.AuthDBContext',同时尝试激活 '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]]'。)(验证服务描述符'ServiceType:Microsoft.AspNetCore.Identity.UserManager1[AuthenticationService.Entities.ApplicationUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserManager
1 [AuthenticationService.Entities.ApplicationUser]'时出错:无法解析类型的服务'AuthenticationService.Data.AuthDBContext' 试图激活 '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.IdentityUse rToken1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim
1[System.String]]'.) AuthenticationService.Data.AuthDBContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim1[AuthenticationService.Entities.ApplicationUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole
1[System.String],Microsoft.AspNetCore.Identity。 IdentityRoleClaim1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken
9[AuthenticationService.Entities.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,AuthenticationService.Data.AuthDBContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim1[System.String]]': Unable to resolve service for type 'AuthenticationService.Data.AuthDBContext' while attempting to activate 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore
1[System.String],Microsoft.AspNetCore .Identity.IdentityUserLogin1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole
1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken
1[Microsoft.AspNetCore.Identity.IdentityRole] 生命周期:Scoped ImplementationType:Microsoft.AspNetCore.Identity.EntityFrameworkCore.RoleStore1[System.String]]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.IRoleStore
1[System.String],Microsoft.AspNetCore.Identity.IdentityRole Claim5[Microsoft.AspNetCore.Identity.IdentityRole,AuthenticationService.Data.AuthDBContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserRole
5[Microsoft.AspNetCore.Identity.IdentityRole,AuthenticationService.Data.AuthDBContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserRole1[System.String]]': Unable to resolve service for type 'AuthenticationService.Data.AuthDBContext' while attempting to activate 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.RoleStore
1[System.String]]'。)(验证服务时出错描述符'ServiceType:Microsoft.AspNetCore.Identity.IUserClaimsPrincipalFactory1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim
9[AuthenticationService.Entities.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,AuthenticationService.Data.AuthDBContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim1[AuthenticationService.Entities.ApplicationUser] Lifetime: Scoped ImplementationType: AuthenticationService.Common.CustomClaimPrincipalFactory': Unable to resolve service for type 'AuthenticationService.Data.AuthDBContext' while attempting to activate 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole
1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim`1[System.String]]'.)1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken
services.AddDbContext<AuthDBContext>();
services.AddIdentity<IdentityUser,IdentityRole>()
.AddEntityFrameworkStores<AuthDBContext>()
.AddDefaultTokenProviders();
/*builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();*/
builder.Services.AddDbContext<ApplicationDbContext>();
builder.Services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();