簡體   English   中英

.NET Core 使用 Azure B2C 身份驗證,登錄后查找 @User.Identity.UserID 等用戶屬性

[英].NET Core using Azure B2C Authentication, looking for the User Attributes such as @User.Identity.UserID after logged in

我正在使用 .NET Core 和 Azure B2C 身份驗證。 在我正在開發的項目中,我可以找到@User.Identity.Name,但我找不到其他“內置”用戶屬性(例如 @User.Identity.UserID 、@User)的 C# 對象名稱/路徑。 Identity.firstTimeLogging,@User.Identity.firstName。 這些屬性在 Azure AD 中啟用。 感謝您的時間。

您可以通過將自定義聲明包含在發送到應用程序的令牌中或通過查詢 Azure AD Graph API(還不是 Microsoft Graph)來訪問自定義聲明。

  • 對於從 claim 中讀取的第一個選項,您可以按照以下線程操作:

https://stackoverflow.com/a/43996372/6049604

  • 對於另一個選項,使用圖形 API 獲取用戶詳細信息,您可以瀏覽以下文檔以獲取相同信息:

https://docs.microsoft.com/en-us/azure/active-directory-b2c/active-directory-b2c-devquickstarts-graph-dotnet?tabs=applications

補充參考:

https://github.com/AzureADQuickStarts/B2C-GraphAPI-DotNet/blob/master/B2CGraphClient/Program.cs

希望能幫助到你。

感謝 Mohit,這是我找到的解決方案。

var claimsIdentity = (System.Security.Claims.ClaimsIdentity)User.Identity;

 List<String> termsList = new List<String>();
    foreach (var claim in claimsIdentity.Claims)
    {
        termsList.Add(claim.Value);
    }
   //The 2nd item in the claim is the objectidentifier
    Debug.WriteLine(termsList[2]); 
// there is probably a cleaner way, but it works for me. 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM