验证服务描述符 'ServiceType: Microsoft.AspNetCore.Identity.IUserClaimsPrincipalFactory`1 时出错

Emm*_*aya 2 entity-framework dependency-injection claims-based-identity asp.net-identity asp.net-core

在此处输入图片说明

首次尝试添加迁移时发生此错误。我已经添加了扩展方法和我的服务类

公共无效配置服务(IServiceCollection 服务){

        services.AddCors();
        services.AddControllers();
        
        services.Configure<AppSettings>(AppSettings);
        services.ConfigureJWT(Configuration);
        services.ConfigureIdentity();
        services.AddScoped<ILoginService, LoginService>();
        services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, CustomClaimPrincipalFactory>();
    }
Run Code Online (Sandbox Code Playgroud)

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();


    }
Run Code Online (Sandbox Code Playgroud)

公共类 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是错误再次

访问 Microsoft.Extensions.Hosting 服务时出错。在没有应用程序服务提供商的情况下继续。错误:无法构建某些服务(验证服务描述符“ServiceType: Microsoft.AspNetCore.Identity.IUserClaimsPrincipalFactory 1[AuthenticationService.Entities.ApplicationUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserClaimsPrincipalFactory1[AuthenticationService.Entities.ApplicationUser]”时出错:无法解析“AuthenticationService.Data.AuthDBContext”类型的服务尝试激活“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]]”时.)(验证服务描述符“ServiceType: Microsoft.AspNetCore.Identity.UserManager”时出错1[AuthenticationService.Entities.ApplicationUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserManager1[AuthenticationService.Entities.ApplicationUser]”:尝试激活“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 ”时,无法解析“AuthenticationService.Data.AuthDBContext”类型的服务。 IdentityUserRole 1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken 1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim1[System.String]]'。)(验证服务描述符“ServiceType: Microsoft.AspNetCore.Identity.IUserStore 1[AuthenticationService.Entities.ApplicationUser] Lifetime: Scoped ImplementationType: 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.IdentityUserLogin1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim 1[System.String]]': Unable to resolve service for type 'AuthenticationService.Data.AuthDBContext' while attempting to activate '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[系统] .String],Microsoft.AspNetCore.Identity.IdentityUserLogin 1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken1 [System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim 1[System.String]]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.IRoleStore1 [Microsoft.AspNetCore.Identity.IdentityRole]效期:作用域ImplementationType: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]]': Unable to resolve service for type 'AuthenticationService.Data.AuthDBContext' while attempting to activate 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.RoleStore5[Microsoft.AspNetCore.Identity.IdentityRole,AuthenticationService.Data.AuthDBContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserRole1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim1[System.String]]'。)(验证服务描述符时出错:“ServiceType: Microsoft.AspNetCore.Identity.IUserClaimsPrincipalFactory 1[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.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]]'。)

小智 10

在配置身份存储之前注册 dbcontext

services.AddDbContext<AuthDBContext>();
services.AddIdentity<IdentityUser,IdentityRole>()
                .AddEntityFrameworkStores<AuthDBContext>()
                .AddDefaultTokenProviders();
Run Code Online (Sandbox Code Playgroud)

  • 如果没有 dbcontext 怎么办?就像使用 dapper 而不是实体框架时一样? (8认同)