使用自定义属性在 Azure AD B2C 中存储有关用户的附加信息

Using custom attributes to store additional information about a user in Azure AD B2C

我想在我的 Azure AD B2C 实例中存储有关用户的其他信息。我所做的是:

  1. 我已经 created a new custom attribute 并且此属性的名称是 Producer

  2. 我已经 added all required permissions 注册了一个新的应用程序注册,旨在通过 Graph API

    使用 Azure AD B2C API
  3. 我调用 Graph API 为其中一个用户设置自定义属性:POST https://graph.microsoft.com/v1.0/users/{user-id} with the following data according to this example

    { "officeLocation": "美国", "extension_XXX_Producer": "一个" }

  4. 当我尝试使用 Graph API: GET https://graph.microsoft.com/v1.0/users/{user-id} 查询有关此用户的信息时,我没有得到任何类似于我的自定义属性的信息

看了Azure AD B2C documentation,好像自定义属性只有添加到其中一个用户流中才能激活,但这不是我们业务想要的。他们希望有另一个 UI 和产品来负责自定义属性管理,这就是为什么我想使用 Graph API 来进行自定义属性管理。

能否向我推荐如何管理自定义属性而不将它们包含在 Azure AD B2C 用户流中?

我还发现了一些人们推荐使用 Azure AD Graph API 的资源,但微软在 Azure 中告诉我这个 API 是遗留的 (我已经检查它并且它有效,但我有一些担心因为 Legacy API):

我查看了您提供的 document example,我注意到该示例是 Azure Active Directory Graph 的演示,因此我建议您也尝试使用 Azure Active Directory Graph。当你使用api查询用户信息时,它看起来像这样:

https://graph.windows.net/{tenant}.onmicrosoft.com/users/{user_id}?api-version = 1.6

在此之前,正如文档所说,您需要为api获取访问令牌,并且在授予权限时,您需要向应用程序授予Azure Active Directory Graph权限。

对于AAD Graph,它是一个较旧的API,只允许访问目录数据,并且它的一些功能已经从AAD Graph迁移到Microsoft Graph。但在某些情况下,我们只能通过AAD Graph来实现需求。

请参阅:The difference between AAD Graph and Microsoft Graph

我做了什么:

  1. 使用 Azure 门户 AD B2C 添加自定义属性(例如生产者)

  2. 登录用户流程

    应用程序声明中添加此属性
  3. 使用图表 API 列出 b2c-extensions-app 的扩展属性。不要修改。 AADB2C用于存储用户数据。 (存储自定义属性的位置,阅读 https://docs.microsoft.com/en-us/azure/active-directory-b2c/extensions-app, https://docs.microsoft.com/en-us/graph/api/resources/extensionproperty?view=graph-rest-beta and https://docs.microsoft.com/en-us/graph/api/application-list-extensionproperty?view=graph-rest-beta&tabs=http)。

    client是一个初始化的MicrosoftGraphClient,appObjectIdObject ID 43=]b2c-extensions-app:

    async function getExtensionProperties(client, appObjectId) {
        return await client
            .api(`/applications/${appObjectId}/extensionProperties`)
            .version('beta')
            .get();
    }
    

    响应应包含如下一行:

    name: 'extension_<Application (client) ID of the b2c-extensions-app without the dashes>_Producer'
    

    这是作为扩展的自定义属性的名称属性。

  4. 使用图形 API 为用户设置自定义属性。

    id是AD中的用户Object ID,attributes{ "extension_<Application (client) ID of the b2c-extensions-app without the dashes>_Producer": "your_value" }

    async function updateUser(client, id, attributes) {
        return await client
            .api(`/users/${id}`)
            .version('beta')
            .header("content-type", "application/json")
            .patch(attributes);
    }
    
  5. 当使用 signin 用户流程登录时,在浏览器中,使用 MSAL,myMSALObj.getAccount().extension_Producer 现在设置为自定义属性值(注意: extension_Producer extensionProducer).[=17= 之间没有应用程序 ID ]

来自 amanpreetsingh-msft 的回答 https://docs.microsoft.com/en-us/answers/questions/21843/how-to-set-custom-claims-for-a-user-in-azure-ad-b2.html 对解决这个问题很有帮助。