简体   繁体   English

如何在使用IdentityServer进行身份验证后获取WebAPI控制器上的用户信息?

[英]How to get user's information on WebAPI controller after authenticated with IdentityServer?

I cannot get user's information on WebAPI controller after my client app authenticates with IdentityServer3 successfully. 在我的客户端应用程序成功验证IdentityServer3后,我无法在WebAPI控制器上获取用户信息。 Below are the steps: 以下是步骤:

  1. "Login With Profile and Access Token" successfully from JavaScript Implicit Client app JavaScript Implicit Client应用程序成功登录“使用配置文件和访问令牌”
  2. I see user's data on "ID Token Contents" panel 我在“ID令牌内容”面板上看到用户的数据 在此输入图像描述
  3. I do "Call service" to my WebAPI service, I see many claims in ClaimsPrincipal but cannot get values such as email, roles displayed on client side. 我对我的WebAPI服务进行“呼叫服务”,我在ClaimsPrincipal中看到了许多声明,但无法获取客户端显示的电子邮件,角色等值。 Below are code & responses. 以下是代码和回复。

在此输入图像描述 Could anyone provide me some helps how to get user's data on WebAPI? 任何人都可以帮我提供一些如何在WebAPI上获取用户数据的帮助吗?

if you use owin, you can try this code. 如果您使用owin,您可以尝试此代码。

 var owinUser = TryGetOwinUser();
 var claim= TryGetClaim(owinUser, "email");
 string email = claim.Value;

 private ClaimsPrincipal TryGetOwinUser()
    {
        if (HttpContext.Current == null)
            return null;

        var context = HttpContext.Current.GetOwinContext();
        if (context == null)
            return null;

        if (context.Authentication == null || context.Authentication.User == null)
            return null;

        return context.Authentication.User;
    }

    private Claim TryGetClaim(ClaimsPrincipal owinUser, string key)
    {
        if (owinUser == null)
            return null;

        if (owinUser.Claims == null)
            return null;

        return owinUser.Claims.FirstOrDefault(o => o.Type.Equals(key));
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .Net Core IdentityServer4获取经过身份验证的用户 - .Net Core IdentityServer4 Get Authenticated User 将JWT发送到新控制器后如何获取用户信息 - How to get user information after sending JWT to a new controller 如何从HTTP筛选器获取经过身份验证的用户名,IP地址和控制器操作? - How to get authenticated user's name, IP address, and the controller action being called from an HTTP Filter? 验证用户身份后如何将变量传递给每个控制器 - How do I pass a variable to every controller after a user is authenticated 检查是否已在IdentityServer 4中对用户进行身份验证 - Check if user is already authenticated in IdentityServer 4 从控制器获取和编辑当前经过身份验证的用户 - Get and edit currently authenticated user from a controller 如何获得最近通过身份验证的用户? - How to get recently authenticated user? 如何在 controller 中使用 IdentityServer4 获取 ASP.NET 核心中的当前用户? - How to get current user in ASP.NET core with IdentityServer4 in a controller? MVC在经过身份验证的页面上显示已验证的用户信息 - MVC Display Authenticated User Information on Authenticated pages 将用户设置为从WebAPI自定义中间件进行身份验证 - Set User as Authenticated from WebAPI custom Middleware
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM