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

限于喜欢 提交于 2019-12-04 05:17:19

问题


I have added a custom Organization field as a User Attribute in my Azure Active Directory B2C tenant, like so:

I am using the Microsoft Graph .NET Client Library to manage users in Azure Active Directory B2C and would like to use something similar to the following code to set the user's custom Organization field and the user's built-in Email Addresses field.

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

Two questions:

  1. How do I set a Built-in field, like the Email Addresses?
  2. How do I set a Custom field, like Organization?

This documentation shows how to create a custom attribute but does not tell how to access or use that attribute using the Graph Client.

This documentation shows how to create custom attributes and edit the Relying Party (RP) file.

Is there an easier way? And what is the graphClient code to then get/set these custom user attributes?


回答1:


It is a bit confusing about whether the Microsoft Graph API, and hence the Microsoft Graph Client, supports the extension properties that are registered with an Azure AD B2C tenant.

When I query a user object using the Azure AD Graph API, then the custom attributes (e.g. "CreatedTime") are returned.

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
}

When I query the same object using the Microsoft Graph API, then the custom attributes aren't returned.

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

returns:

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

Unless you receive a better answer, then I suggest you use the Azure AD Graph API, and optionally the Azure AD Graph Client, to get and set the extension properties for the Azure AD B2C users.

Examples of getting and setting the extension properties for users can be found at Announcing Azure AD Graph API Client Library 2.0



来源:https://stackoverflow.com/questions/48769708/how-to-get-set-custom-azure-active-directory-b2c-user-attributes-in-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!