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 控制


- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 输入按键事件处理程序 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01