且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用图形API更新Azure Ad B2c自定义用户属性

更新时间:2023-02-16 08:38:37

请检查以下代码更改,并验证您尝试更新的用户是否具有自定义属性

Please check the below code changes and also verify whether the user you are trying update having the custom attribute or not

public static async Task UpdateCustomAtrributeUserId(GraphServiceClient graphClient)
        {
            Console.Write("Enter user object ID: ");
            string userId = Console.ReadLine();
            string CustomAtrribute = "B2C_Custom_AtrributeName";

 

            Console.WriteLine($"Looking for user with object ID '{userId}'...");

 

            try
            {
              //Get User details to Verify the existing values
                var result = await graphClient.Users[userId]
                  .Request()
                  .Select($"id,givenName,surName,displayName,identities,{CustomAtrribute}")
                  .GetAsync();

 

                Console.WriteLine(result);

 

                if (result != null)
                {
                    //Enter the New custom attribute value
                    Console.WriteLine("Enter custom attribute value");
                    string updatecustomeattribvalue = Console.ReadLine();
                    
                    //Fill custom attribute value
                    IDictionary<string, object> extensionInstance = new Dictionary<string, object>();
                    extensionInstance.Add(CustomAtrribute, updatecustomeattribvalue);
                    //Updating the custom attribute 
                   var updatedresult  = await graphClient.Users[userId]
                            .Request()
                            .UpdateAsync(new User {
                                AdditionalData = extensionInstance
                            });
                   
                    Console.WriteLine(JsonConvert.SerializeObject(updatedresult));
                }
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.Message);
                Console.ResetColor();
            }
        }