How to get user#39;s information on WebAPI controller after authenticated with IdentityServer?(通过 IdentityServer 身份验证后如何获取 WebAPI 控制器上的用户信息?)
问题描述
在我的客户端应用程序成功通过 IdentityServer3 进行身份验证后,我无法在 WebAPI 控制器上获取用户信息.步骤如下:
I cannot get user's information on WebAPI controller after my client app authenticates with IdentityServer3 successfully. Below are the steps:
- 使用配置文件和访问令牌登录"成功从 JavaScript隐式客户端应用程序
- 我在ID Token Contents"面板上看到了用户数据
- 我对我的 WebAPI 服务执行呼叫服务",我在 ClaimsPrincipal 中看到许多声明,但无法获取电子邮件、客户端显示的角色等值.下面是代码&回复.
谁能帮助我如何在 WebAPI 上获取用户数据?
Could anyone provide me some helps how to get user's data on WebAPI?
推荐答案
如果你使用owin,可以试试这段代码.
if you use owin, you can try this code.
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));
}
这篇关于通过 IdentityServer 身份验证后如何获取 WebAPI 控制器上的用户信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:通过 IdentityServer 身份验证后如何获取 WebAPI 控制


基础教程推荐
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01