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

不羁岁月 提交于 2020-01-25 10:19:07

问题


I'm using .NET Core, with Azure B2C Authentication. On the project I'm developing, I can find the @User.Identity.Name, but I cant find the C# object name/path for the other "built in" User Attributes such as @User.Identity.UserID , @User.Identity.firstTimeLogging, @User.Identity.firstName. These Attribute are enabled in Azure AD. Thank you for your time.


回答1:


You can access custom claims by including them in the token sent to the app or by querying the Azure AD Graph API (not the Microsoft Graph yet).

  • For the first option reading it from claim , you can follow below thread :

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

  • For the other option , using graph api to get user details , you can browse the below doc for the same:

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

Additional reference:

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

Hope it helps.




回答2:


Thanks to Mohit, this is the solution I found.

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. 


来源:https://stackoverflow.com/questions/59367686/net-core-using-azure-b2c-authentication-looking-for-the-user-attributes-such-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!