本文介紹了通過 IdentityServer 身份驗證后如何獲取 WebAPI 控制器上的用戶信息?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
在我的客戶端應用程序成功通過 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 控制器上的用戶信息?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!