Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure AD GraphServiceClient can't set AdditionalData against User

Tags:

I am using the GraphServiceClient with .Net Core 2. I am trying to add AdditionalData to my Users using the following code

var updated = new User()
                {
                   AdditionalData = new Dictionary<string, object>
                    {
                        {"OtherEmail", otherEmail},
                        {"OtherRole", otherRole}
                    },
                };

                await _graphClient.Users[user.Id].Request().UpdateAsync(updated);

When this executes I get the following error

Microsoft.Graph.ServiceException : Code: Request_BadRequest Message: One or more property values specified are invalid.

Does anyone know what I am doing wrong ?

Also if anyone can please tell me how I can just save some of my own metadata against a user it would be really appreciated. I have also tried using the extensions, but I am having this problem.

Azure AD GraphServiceClient Extensions not working

like image 457
Lenny D Avatar asked Dec 07 '18 13:12

Lenny D


2 Answers

To add attributes to the AdditionalData field you have to define the attribute in Azure AD B2C > User attributes (in the Azure portal). If you for example create a Boolean attribute Dummy, this will create the field extension_{b2c-extensions-app-id}_Dummy where {b2c-extensions-app-id} is the Application ID of the automatically generated b2c-extensions-app viewable by navigating to Azure AD B2C > App registrations (Preview) > b2c-extensions-app with the dashes (-) removed from the ID!

This would allow you to do the following:

var updated = new User()
    {
        AdditionalData = new Dictionary<string, object>
        {
            {"extension_0000000000000000000000000000_Dummy", false}
        },
    };

await _graphClient.Users[user.Id].Request().UpdateAsync(updated);
like image 172
Tiedye Avatar answered Oct 23 '22 10:10

Tiedye


So I got around this using OpenExtensions, this also caused me a lot of issues, which can be seen here.

Azure AD GraphServiceClient Extensions not working

Trick is to add the extensions like this

extension = new OpenTypeExtension
                {
                    ExtensionName = AzureADExtensions.UserConstants.ExtensionName,
                    AdditionalData = new Dictionary<string, object>
                    {
                        {"OtherEmail", externalUser.Email},
                        {"OtherRole" , externalUser.Roles.FirstOrDefault()}
                    }
                };

                await _graphClient.Users[user.Id].Extensions.Request()
                    .AddAsync(extension);

And then retrieve them like this.

user = await _graphClient
                    .Users[userId]
                    .Request()
                    .GetAsync();
// Note : you should be able to expand this on original request, but fails for me.
                var extensions = await _graphClient.Users[user.Id].Extensions.Request().GetAsync();
                user.Extensions = extensions;

Hope that helps someone!

like image 45
Lenny D Avatar answered Oct 23 '22 10:10

Lenny D