首页 > 解决方案 > C# / Azure AD / Graph API - 使用 Open Extensions 设置其他用户属性

问题描述

我正在尝试使用 Microsoft Graph 向我在 Azure Active Directory 中新创建的用户对象添加一些自定义属性。

我的代码如下所示:

public async Task addUser(string firstName, string lastName)
        {
            var mail = firstName.ToLower() + "." + lastName.ToLower() + "@mail.com";
            var user = new User
            {
                AccountEnabled = true,
                UserPrincipalName = mail,
                PasswordProfile = new PasswordProfile
                {
                    ForceChangePasswordNextSignIn = true,
                    Password = randomString(10) // I wrote a dedicated function here
                },
            };


            var res = await graphServiceClient
                .Users
                .Request()
                .AddAsync(user);

            await addExtension(res.Id);
        }
public async Task addExtension(string id)
        {
            var extension = new OpenTypeExtension
            {
                ExtensionName = "com.test.test", // necessary I guess
                AdditionalData = new Dictionary<string, object>
                    {
                        {"NewAttribute1", "Batman"},
                        {"NewAttribute2" , "Spiderman"}
                    }
            };


            await graphServiceClient
               .Users[id]
               .Extensions
               .Request()
               .AddAsync(extension);
        }

我目前收到的错误消息是:

Message: One or more properties contains invalid values.
Inner error:
    AdditionalData:

...

我在这里定位了自己:

https://docs.microsoft.com/en-us/graph/api/opentypeextension-post-opentypeextension?view=graph-rest-1.0&tabs=http

希望有人能帮忙,谢谢!

标签: c#graphazure-active-directory

解决方案


我设法使我的程序工作。上面的代码实际上是正确的,经过一些调整,现在对我来说很好。

我必须单独阅读扩展名,如下所示。访问该user.Extensions属性对我不起作用。希望这对将来的某人有所帮助:)

public async Task getUserExtensions(string userID, string extensionID)
        {
            var extension = await configuration
                .configure()
                .Users[userID]
                .Extensions[extensionID]
                .Request()
                .GetAsync();

            foreach (var item in extension.AdditionalData)
            {
                Console.WriteLine($"{item.Key} | {item.Value}");
            }
        }

推荐阅读