如何 get/set 在 ASP.NET MVC 中自定义 Azure Active Directory B2C 用户属性?

How to get/set custom Azure Active Directory B2C user attributes in ASP.NET MVC?

我在我的 Azure Active Directory B2C 租户中添加了一个自定义 Organization 字段作为用户属性,如下所示:

我正在使用 Microsoft Graph .NET Client Library 来管理 Azure Active Directory B2C 中的用户,并且想使用类似于以下代码的东西来设置用户的自定义 Organization 字段和用户的内置Email Addresses 字段。

await graphClient.Users[user.Id].Request().UpdateAsync(new User()
{
    Email Addresses = new StringCollection("myemail@mydomain.com")
    Organization = "Microsoft"
});

两个问题:

  1. 如何设置内置字段,例如 Email Addresses
  2. 如何设置自定义字段,例如 Organization

此文档显示 how to create a custom attribute 但未说明如何使用 Graph Client 访问或使用该属性。

本文档展示了如何 create custom attributes and edit the Relying Party (RP) file

有没有更简单的方法?然后 get/set 这些自定义用户属性的 graphClient 代码是什么?

Microsoft Graph API 以及 Microsoft Graph 客户端是否支持向 Azure AD B2C 租户注册的扩展属性,这有点令人困惑。

当我使用 Azure AD Graph API 查询用户对象时,会返回自定义属性(例如 "CreatedTime")。

https://graph.windows.net/{tenant}/users/{objectId}

returns:

{
    "odata.metadata": "https://graph.windows.net/{tenant}/$metadata#directoryObjects/Microsoft.DirectoryServices.User/@Element",
    "odata.type": "Microsoft.DirectoryServices.User",
    "objectType": "User",
    ...
    "extension_917ef9adff534c858b0a683b6e6ec0f3_CreatedTime": 1518602039
}

当我使用 Microsoft Graph API 查询同一对象时,未返回自定义属性。

https://graph.microsoft.com/v1.0/users/{id}/extensions

returns:

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('{id}')/extensions",
    "value": []
}

除非您收到更好的答案,否则我建议您使用 Azure AD Graph API,并可选择 the Azure AD Graph Client 来获取和设置 Azure AD B2C 用户的扩展属性。

可以在 Announcing Azure AD Graph API Client Library 2.0

找到获取和设置用户扩展属性的示例

您可以使用 Micorsoft Graph API SDK 执行此操作。

看到这个例子,UserService.CreateUserWithCustomAttribute() https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/tree/master/4-WebApp-your-API/4-2-B2C

更新自定义 属性:

var updateUser = new User();
updateUser.AdditionalData = new Dictionary<string, object>();
updateUser.AdditionalData["extension_{app id}_{property name}"] = "new value";

var result = await graphClient.Users["{id}"].Request().UpdateAsync(updateUser);

上面代码中的{app id}是默认创建的app的id,名字是b2c-extensions-app. Do not modify. Used by AADB2C for storing user data.但是“-”都去掉了。

除了 Aaron Hoffman 关于如何设置自定义属性的回答之外,我还使用以下代码片段来获取我的属性:

var graphClient = new GraphServiceClient(authenticationProvider)
{
    BaseUrl = "https://graph.microsoft.com/beta"
};
var user = await graphClient
    .Users["{id}"]
    .Request()
    .GetAsync();
var field = user.AdditionalData["extension_{app id}_{property name}"];

所以第一步是在您的自定义策略中找到:

<TechnicalProfile Id="AAD-Common">...<Item Key="ClientId">57ff56e7-40a0-43fd-a9a3-8d6c1544bcf4a</Item>

自定义属性被命名为 extension_attributename。 要通过 graphql 获取它,您将像这样需要它 例如extension_57ff56e740a043fda9a38d6c1544bcf4a_mycoolattribute 如您所见,这也在代码中完成: https://github.com/Azure-Samples/ms-identity-dotnetcore-b2c-account-management/blob/master/src/Helpers/B2cCustomAttributeHelper.cs#L7-L20 图调用示例:https://graph.microsoft.com/v1.0/users/3545c38b-3f6b-4a4b-8820-e7f954a86e1e?$select=extension_57ff56e740a043fda9a38d6c1544bcf4a_mycoolattribute https://graph.microsoft.com/v1.0/users/{user-objectid}?$select=extension_57ff56e740a043fda9a38d6c1544bcf4a_mycoolattribute,extension_57ff56e740a043fda9a38d6c1544bcf4a_myotherattribute,etc