[英]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:
以下是步骤:
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.