如何使用 Graph API 从 B2C 检索我的自定义用户属性(声明)?

How can I retrieve my Custom User Attributes (Claims) from B2C using Graph API?

我已经成功创建了一个使用新 B2C 租户进行身份验证的应用程序,我可以登录并调用 APIs。

我为用户添加了新的属性(角色是其中之一)。 在我的“用户流程”中,我有 1 个登录流程,其中选择了角色声明一个用于登录,一个用于编辑用户。在下图中,您可以看到使用用户流程编辑后的设置和返回的声明。

Added extended user attribute

Selected to return data as claim

Decoded Token with extension claims and values

但是,当我为用户 + 扩展查询 Graph PI 时,没有返回扩展。

我还可以使用 Graph API 访问用户,并且可以使用 Graph API 通过代码向用户添加扩展。我可以添加扩展属性、更新它们、检索它们... 在下面的代码中,我可以添加它们。这些使用图表创建的 API 我可以稍后检索,但不是我使用 B2C 用户流程创建的那些。

    // Add extension without the guid from b2c_extensions_app        
    var extension = new OpenTypeExtension()
    {
        ExtensionName = $"extension_Roles",
        AdditionalData = new Dictionary<string, object>()
        {
            { "Roles", value}
        }
    };
    await _graphServiceUsers[userId].Extensions.Request().AddAsync(extension);


    // Add extension with the guid from b2c_extensions_app  
    var extension2= new OpenTypeExtension()
    {
        ExtensionName = $"extension_00000000-e062-0000-8ec8-800000000003_Roles",
        AdditionalData = new Dictionary<string, object>()
        {
            { "Roles", value}
        }
    };

    await _graphServiceUsers[userId].Extensions.Request().AddAsync(extension2);

以上两种情况均未链接到随声明一起返回的 B2C 用户。

我可以使用以下代码检索具有扩展名的用户。我得到了从 Graph API 创建的扩展,但不是上图中令牌声明中显示的扩展。

_graphServiceUsers[userId].Request().Select(UserSelectExpression).Expand("extensions").GetAsync();

所有自定义属性似乎都已断开连接。我需要更新作为声明返回的那些,因为它们是我想用图表 API.

修改的那些

如果您看到下图,您可以看到扩展属性是如何出现的,但不是我从 B2C 添加的那些。在令牌中,您可以看到映射的声明具有一个值,不同于我从 Graph API.

添加和检索的值

Different values from Claims and retrieved data using Graph API

我认为他们只是让它变得太难了,而现实应该更容易实现像添加新属性这样简单的事情并检索值并使用 Graph API.


附加信息:

我正在使用 Graph API 应用程序访问 api

B2C Applications

我添加了所有建议的权限

Graph API permissions

在 Graph Explorer 中(与 C# 相同的结果):

https://graph.microsoft.com/v1.0/users/600001c5-0000-49b9-89c5-0000c80000fc?$select=displayName,identities,extension_39b4801c-5782-48d4-be6a-1cae6a8a881a_Roles

结果(异常): Result Graph Explorer

Result C#

------------更新解决方案-------------

我只是想添加一个简短的注释来澄清真正的问题,以防万一有人遇到同样的问题,因为开始使用 Graph API。

问题(截至今天)是您需要使用 Beta 版的 Nuget 包或 Beta (graph.microsoft.com/beta) 图 api 来检索所有扩展属性。稳定版不行

  1. 对我有用的 nuget 包是 Microsoft.Graph.Beta 4.21.0-preview。
  2. 2021 年 11 月 25 日发布的包 Microsoft.Graph 稳定版 4.11.0 未检索扩展属性。

注意:重要的是扩展名 extension_39b0000c570000d4be6a1cae7a9a000a_Roles 没有来自 guid 的 -。

另外,要记住的另一件事是使用扩展中的 guid 来仅加载扩展: 第一个为用户加载所有属性。 第二个只加载自定义扩展属性。

    var user = await _graphServiceClient.Users[userId].Request().GetAsync();
    
    
    var user2 = await _graphServiceClient.Users[userId].Request().Select("id,extension_39b0000c570000d4be6a1cae7a9a000a_Roles").GetAsync();

根据我的测试,我可以使用此请求来获取扩展属性:

https://graph.microsoft.com/v1.0/users/198axxxxx9ce1c?$select=id,country,extension_3d3xxxxx707e_tiny_custom_prop

测试结果是

并带有 asp.net 核心代码,遵循 this document:

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

// Values from app registration
var clientId = "application_clientid_that_registered_in_b2c";
var clientSecret = "app_client_secret";
var options = new TokenCredentialOptions
{
                AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};
var clientSecretCredential = new ClientSecretCredential(
                tenantId, clientId, clientSecret, options);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
var user = graphClient.Users["198xxxxx9ce1c"].Request()
                    .Select("id,country,extension_3dxxxxx7e_tiny_custom_prop")
                    .GetAsync();
var res = user.Result;

===========================更新================= ===========

你可以看到声明也是一个自定义属性,我得到了我特定用户的这个 属性 的值。我这里查询的用户是我重定向到b2c注册页面时注册的用户,需要用户流程来设置这个自定义属性.

我创建了一个 azure ad b2c 应用程序并为此应用程序设置了这些 api 权限。

我在我的解决方法中使用了 v1.0 版本,所以我需要使用 $select 特性来在我的代码中获取扩展 属性,但是如果我在这里使用 beta 版本,我可以得到直接扩展 属性。