Azure B2C - 如何从 Microsoft Graph 获取用户详细信息?

标签 azure azure-ad-b2c azure-ad-graph-api

我正在学习/试验 Azure Active Directory B2C (AADB2C)。我创建了一个 Web 应用程序,可以使用 OpenID Connect 登录或注册用户(不使用自定义用户流程)。

用户登录后,我想调用 Microsoft Graph 以获取有关登录用户的信息。

根据文档,我初始化客户端凭据身份验证提供程序,如下所示:

var scopes = new[] { "https://graph.microsoft.com/.default" };

var clientSecretCredential = new ClientSecretCredential(config.TenantId, config.AppId, config.ClientSecret);

var graphClient = new GraphServiceClient(clientSecretCredential, scopes);

然后我尝试提出请求,如下所示:

var user = await graphClient.Me.Request().GetAsync();

上述结果导致异常:

Code: BadRequest
Message: /me request is only valid with delegated authentication flow.

当我尝试以下操作时:

var user = await graphClient.Users[ '' ].Request().GetAsync();

上述结果导致异常:

Code: Authorization_RequestDenied
Message: Insufficient privileges to complete the operation.

很多MS文档中的示例都说明必须授予“User.Read”权限,但是在使用Azure Portal时找不到“User.Read”权限,下面是Azure 门户中“请求 API 权限”边栏选项卡的屏幕快照 - 我仍然不清楚注释(在下图中突出显示)。

Azure Portal trying to add User.Read permission

当我尝试输入“User.Read”权限时,会显示以下内容:

User.Read permission not found

我还找到了关于 how to Register a Microsoft Graph application 的教程,但它似乎适合想要访问图表的管理员,而不是“常规”登录用户本身。

我的问题 - 是否有任何工作示例说明如何(在用户登录后)从 Microsoft Graph 读取用户信息?

最佳答案

Note that, web applications registered with supported account type as Accounts in any identity provider or organizational directory (for authenticating users with user flows) supports only Microsoft Graph offline_access and openid delegated permissions.

我尝试在我的环境中重现相同的结果并得到以下结果:

我通过选择支持的帐户类型在我的 B2C 租户中注册了一个应用程序,如下所示:

enter image description here

当我使用相同的代码从客户端凭据流获取登录用户配置文件时,我收到相同的错误,因为此流不支持委派权限。

using Microsoft.Graph;
using Azure.Identity;

var scopes = new[] { "https://graph.microsoft.com/.default" };
var tenantId = "srib2corg.onmicrosoft.com";
var clientId = "7427462d-b6e1-4ab0-ba0c-xxxxxxxx";
var clientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
var clientSecretCredential = new ClientSecretCredential(
                tenantId, clientId, clientSecret);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
var user = await graphClient.Me.Request().GetAsync();

回应:

enter image description here

以下命令是获取我遇到相同错误的用户列表,因为我没有所需的权限,如下所示:

var user = await graphClient.Users["userID"].Request().GetAsync();

回应:

enter image description here

要获取登录的用户个人资料,您需要在 B2C 应用程序中添加委托(delegate)的 User.Read 权限,但由于您受支持的帐户,该权限不可用类型:

enter image description here

解决此问题,您需要使用支持的帐户类型作为单租户/ Multi-Tenancy 来创建应用注册,如下所示:

enter image description here

在上面的应用程序中,您可以添加委托(delegate)User.Read权限,如下所示:

enter image description here

由于客户端凭据流程不适用于委派权限,因此您需要将流程更改为委派流程,例如授权码、用户名密码等...

就我而言,我更改为用户名密码流程并使用以下代码来获取用户个人资料:

using Microsoft.Graph;
using Azure.Identity;

var scopes = new[] { "https://graph.microsoft.com/.default" };
var tenantId = "srib2corg.onmicrosoft.com";
var clientId = "7d983e16-5296-4056-8e72-xxxxxxxx";

var options = new TokenCredentialOptions
{
    AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};

var userName = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="49282d242027093a3b202b7b2a263b2e67262724202a3b263a262f3d672a2624" rel="noreferrer noopener nofollow">[email protected]</a>";
var password = "xxxxxxxxx";

var userNamePasswordCredential = new UsernamePasswordCredential(
    userName, password, tenantId, clientId, options);

var graphClient = new GraphServiceClient(userNamePasswordCredential, scopes);

var user = await graphClient.Me.Request().GetAsync();

Console.WriteLine("\nName: " + user.DisplayName);
Console.WriteLine("\nID: " + user.Id);
Console.WriteLine("\nUPN: " + user.UserPrincipalName);

回应:

enter image description here

如果您的用例是在用户登录网络应用程序后获取个人资料,您可以查看下面我最近回答的SO thread

Signed-in user Profile page in ASP.NET Core Web App calling the Microsoft Graph - Stack Overflow

关于Azure B2C - 如何从 Microsoft Graph 获取用户详细信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75077793/

相关文章:

azure - 调整 Azure 托管数据磁盘大小时出现问题

Azure 媒体服务视频无法在 Android 和 iOS Real Devices React Native 上运行

azure-ad-b2c - Azure AD B2C 多步骤自定义策略

Azure Graph API 列出子组中的用户

office365 - 之前有效的 Microsoft Graph 请求出现 InefficientFilter 错误

azure - 网站的 web.config 加密

azure - 如何在azure中的webjobs之间共享配置文件

Azure AD B2C - 忘记密码用户旅程 - 不允许旧密码?

azure - Azure Blob 存储中的 HTML 无法加载图标(错误 400)

c# - 使用 Graph Api 针对租户进行角色计数