[英]How to get user's information on WebAPI controller after authenticated with IdentityServer?
在我的客戶端應用程序成功驗證IdentityServer3后,我無法在WebAPI控制器上獲取用戶信息。 以下是步驟:
任何人都可以幫我提供一些如何在WebAPI上獲取用戶數據的幫助嗎?
如果您使用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.