本文介紹了通過 IdentityServer 身份驗證后如何獲取 WebAPI 控制器上的用戶信息?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
限時送ChatGPT賬號..
在我的客戶端應(yīng)用程序成功通過 IdentityServer3 進(jìn)行身份驗證后,我無法在 WebAPI 控制器上獲取用戶信息.步驟如下:
I cannot get user's information on WebAPI controller after my client app authenticates with IdentityServer3 successfully. Below are the steps:
- 使用配置文件和訪問令牌登錄"成功從 JavaScript隱式客戶端應(yīng)用程序
- 我在ID Token Contents"面板上看到了用戶數(shù)據(jù)
- 我對我的 WebAPI 服務(wù)執(zhí)行呼叫服務(wù)",我在 ClaimsPrincipal 中看到許多聲明,但無法獲取電子郵件、客戶端顯示的角色等值.下面是代碼&回復(fù).
誰能幫助我如何在 WebAPI 上獲取用戶數(shù)據(jù)?
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));
}
這篇關(guān)于通過 IdentityServer 身份驗證后如何獲取 WebAPI 控制器上的用戶信息?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!