在我的客户端应用程序成功通过 IdentityServer3 进行身份验证后,我无法在 WebAPI 控制器上获取用户信息.步骤如下:
I cannot get user's information on WebAPI controller after my client app authenticates with IdentityServer3 successfully. Below are the steps:
谁能帮助我如何在 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 控制器上的用户信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!