简体   繁体   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

I'm using .NET Core, with Azure B2C Authentication.我正在使用 .NET Core 和 Azure B2C 身份验证。 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.在我正在开发的项目中,我可以找到@User.Identity.Name,但我找不到其他“内置”用户属性(例如 @User.Identity.UserID 、@User)的 C# 对象名称/路径。 Identity.firstTimeLogging,@User.Identity.firstName。 These Attribute are enabled in Azure AD.这些属性在 Azure AD 中启用。 Thank you for your time.感谢您的时间。

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).您可以通过将自定义声明包含在发送到应用程序的令牌中或通过查询 Azure AD Graph API(还不是 Microsoft Graph)来访问自定义声明。

  • For the first option reading it from claim , you can follow below thread :对于从 claim 中读取的第一个选项,您可以按照以下线程操作:

https://stackoverflow.com/a/43996372/6049604 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:对于另一个选项,使用图形 API 获取用户详细信息,您可以浏览以下文档以获取相同信息:

https://docs.microsoft.com/en-us/azure/active-directory-b2c/active-directory-b2c-devquickstarts-graph-dotnet?tabs=applications 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 https://github.com/AzureADQuickStarts/B2C-GraphAPI-DotNet/blob/master/B2CGraphClient/Program.cs

Hope it helps.希望能帮助到你。

Thanks to Mohit, this is the solution I found.感谢 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.

相关问题 如何使用 MSAL C#、.net 核心在 Azure AD B2C 用户帐户上重置密码? - How to reset password on Azure AD B2C user account using MSAL C#, .net core? Azure B2C 和 .net 核心 3.1 认证问题 - Azure B2C and .net core 3.1 Authentication Problem .NET Core API Azure B2C认证配置 - .NET Core API Azure B2C authentication configuration 如何在 ASP.NET MVC 中获取/设置自定义 Azure Active Directory B2C 用户属性? - How to get/set custom Azure Active Directory B2C user attributes in ASP.NET MVC? Azure B2C 检查用户 email Id 是否存在或不使用 ASP.NET Core 3.1 MVC 中的图表 - Azure B2C check user email Id exists or not using Graph in ASP.NET Core 3.1 MVC 使用用户分配的托管标识访问 Azure AD B2C 和 MS Graph API - Access Azure AD B2C with MS Graph API using User-Assigned Managed Identity 使用 Azure AD B2C 使用个人用户帐户登录 ASP.Net Core Web 应用程序 -&gt; API - Sign in to ASP.Net Core Web Application -> API with Individual User Accounts using Azure AD B2C Azure AD B2C.Net SDK - 通过 Email 检索用户 - Azure AD B2C .Net SDK - Retrieve User by Email 将 Azure B2C 与 MVC、.NET Core 3.1 结合使用 - Using Azure B2C with MVC, .NET Core 3.1 如何检查当前登录的用户是否是Azure Active Directory B2C“全局管理员”? ASP.NET MVC - How to check if currently logged-in user is Azure Active Directory B2C “Global Administrator”? ASP.NET MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM