简体   繁体   中英

How to update azure ad b2c custom user attribute using graph api

I'm new to the Azure B2C .I created custom attribute extension_role against a user object.i want to update this attribute using graph api.I tried below code

public async Task UpdateUsersRole(string id)
        {
         IDictionary<string, object> extensionInstance = new Dictionary<string, object>();
            extensionInstance.Add("extension_role", "admin");
            var user = new User
            {
          
                AdditionalData = extensionInstance

            };

            await  GraphClient.Users[id]
                .Request()
                .UpdateAsync(user);
        }

is that correct way to update the custom attribute?.While executing i got an error also

Code: Request_BadRequestMessage: One or more property values specified are invalid.Inner error

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();
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM