在验证服务描述符时发生错误:'ServiceType: Microsoft.AspNetCore.Identity.IUserClaimsPrincipalFactory`1'。

8

enter image description here

这个错误是在第一次尝试添加迁移时出现的。我已经添加了扩展方法和我的服务类。
公共 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.UserClaimsPrincipalFactory1[AuthenticationService.Entities.ApplicationUser]':在激活'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore9[AuthenticationService.Entities.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,AuthenticationService.Data.AuthDBContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim1[System.String]]'时无法解析类型为'AuthenticationService.Data.AuthDBContext'。)(验证服务描述符时出错'ServiceType: Microsoft.AspNetCore.Identity.UserManager1[AuthenticationService.Entities.ApplicationUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserManager1[AuthenticationService.Entities.ApplicationUser]':在激活'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore9[AuthenticationService.Entities.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,AuthenticationService.Data.AuthDBContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim1[System.String]]'时无法解析类型为'AuthenticationService.Data.AuthDBContext'。)(验证服务描述符时出错'ServiceType: Microsoft.AspNetCore.Identity.IUserStore1[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.IdentityUserClaim1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim1[System.String]]':在激活'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore9[AuthenticationService.Entities.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,AuthenticationService.Data.AuthDBContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim1[System.String]]'时无法解析类型为'AuthenticationService.Data.AuthDBContext'。)(验证服务描述符时出错'ServiceType: Microsoft.AspNetCore.Identity.IRoleStore1[Microsoft.AspNetCore.Identity.IdentityRole] Lifetime: Scoped ImplementationType: 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]]':在激活'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]]'时无法解析类型为'AuthenticationService.Data.AuthDBContext'。)(验证服务描述符时出错'ServiceType: Microsoft.AspNetCore.Identity.IUserClaimsPrincipalFactory1[AuthenticationService.Entities.ApplicationUser] Lifetime: Scoped ImplementationType: AuthenticationService.Common.CustomClaimPrincipalFactory':在激活'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore9[AuthenticationService.Entities.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,AuthenticationService.Data.AuthDBContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim1[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]]'时无法解析类型为'AuthenticationService.Data.AuthDBContext'。)
4个回答

29

在配置 Identity 存储之前,先注册 dbcontext。

services.AddDbContext<AuthDBContext>();
services.AddIdentity<IdentityUser,IdentityRole>()
                .AddEntityFrameworkStores<AuthDBContext>()
                .AddDefaultTokenProviders();

7
如果你没有 DbContext,比如使用 Dapper 而不是 Entity Framework,会怎样呢? - symbiont
我和@symbiont有相同的查询。 - sanampakuwal
这些需要仔细研究,因为Dapper是另一个完全不同的用例。如果我能在这方面取得任何有意义的进展,我会分享更新。 - wsduho
如果我将AddDefaultIdentity<1>更改为AddIdentity<1,2>,那么所有脚手架标识页面(即Account / Login)都是空白的。有什么想法吗?脚手架是否基于program.cs的内容? - Michael

4

对于 .NET Core 6.0:

/*builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();*/
builder.Services.AddDbContext<ApplicationDbContext>();
builder.Services.AddIdentity<IdentityUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

1

在 .net 7.0 和 6.0 中,只需添加以下内容

builder.Services.AddAuthentication();

0
在builder.Services.AddIdentity之前添加以下行
    var connectionString =                 
    builder.Configuration.GetConnectionString("connectionstring");
    builder.Services.AddDbContext<identitycontext>(options =>
    options.UseOracle(connectionString));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接