如何使用另一个项目的 IdentityServer4 UserManager 方法 api

How to use IdentityServer4 UserManager method from another project api

我在目录 D:\IdentityServer 中有一个身份服务器项目,在目录 D:\WebApi 中有一个 api 项目 - Web api 正在使用另一个数据库并且由身份服务器验证。

我试过:

private readonly UserManager<IdentityUser> _userManager;


public GetProductsHandler(UserManager<IdentityUser> userManager) 
{}

但我明白了

Error constructing handler for request of type MediatR.IRequestHandler2[TestService.Queries.GetProductsHandler1[TestSerice.Dtos.ProductsDto]]. Register your handlers with the container. See the samples in GitHub for examples.

如何调用 UserManager(从 Identity Server)通过用户名获取用户信息或使用获取用户角色?

在您的 API 中,您需要将所有身份对象注入 Startup.cs:

        services.AddScoped<IUserValidator<ApplicationUser>, UserValidator<ApplicationUser>>();
        services.AddScoped<IPasswordValidator<ApplicationUser>, PasswordValidator<ApplicationUser>>();
        services.AddScoped<IPasswordHasher<ApplicationUser>, PasswordHasher<ApplicationUser>>();
        services.AddScoped<ILookupNormalizer, UpperInvariantLookupNormalizer>();
        services.AddScoped<IRoleValidator<ApplicationRole>, RoleValidator<ApplicationRole>>();
        services.AddScoped<IdentityErrorDescriber>();
        services.AddScoped<ISecurityStampValidator, SecurityStampValidator<ApplicationUser>>();
        services.AddScoped<ITwoFactorSecurityStampValidator, TwoFactorSecurityStampValidator<ApplicationUser>>();
        services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, UserClaimsPrincipalFactory<ApplicationUser, ApplicationRole>>();
        services.AddScoped<ApplicationUserManager>();
        services.AddScoped<ApplicationRoleManager>();
        services.AddScoped<ApplicationSignInManager>();
        services.AddScoped<UserManager<ApplicationUser>>();
        services.AddScoped<SignInManager<ApplicationUser>>();
        services.AddScoped<RoleManager<ApplicationRole>>();
        services.AddScoped<IUserStore<ApplicationUser>, ApplicationUserStore>();
        services.AddScoped<IRoleStore<ApplicationRole>, ApplicationRoleStore>();
        services.AddScoped<IUserConfirmation<ApplicationUser>, DefaultUserConfirmation<ApplicationUser>>();
        var identityBuilder = new IdentityBuilder(typeof(ApplicationUser), typeof(ApplicationRole), services);
        identityBuilder.AddTokenProvider("Default", typeof(DataProtectorTokenProvider<ApplicationUser>));