本文介绍了如何使用来自另一个项目 api 的 IdentityServer4 UserManager 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我在 D:IdentityServer 目录中有一个身份服务器项目,在 D:WebApi 目录中有一个 api 项目 - web api 正在使用另一个数据库并通过身份服务器进行身份验证.

I have an identity server project located in the directory D:IdentityServer and have an api project located in the directory D:WebApi - web api is using another database and authenticated by identity server.

我试过了:

private readonly UserManager<IdentityUser> _userManager;


public GetProductsHandler(UserManager<IdentityUser> userManager)
{}

但我明白

为 MediatR.IRequestHandler 类型的请求构建处理程序时出错2[TestService.Queries.GetProductsHandler1[TestSerice.Dtos.ProductsDto]].向容器注册您的处理程序.有关示例,请参阅 GitHub 中的示例.

如何调用 UserManager(来自 Identity Server)以通过用户名获取用户信息或用于获取用户角色?

How can I call UserManager (from Identity Server) to get a user's information by username or use to get the user's role?

推荐答案

在你的 API 中,你需要在 Startup.cs 中注入所有的标识对象:

In your API, you need to inject all of the identity objects inside 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>));

这篇关于如何使用来自另一个项目 api 的 IdentityServer4 UserManager 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 17:06