如何使用图形API更新Azure Ad B2c自定义用户属性 [英] How to update azure ad b2c custom user attribute using graph api

本文介绍了如何使用图形API更新Azure Ad B2c自定义用户属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Azure B2C的新手.我针对用户对象创建了自定义属性extension_role.我想使用图形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

代码:Request_BadRequestMessage:指定了一个或多个属性值无效.内部错误

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

这篇关于如何使用图形API更新Azure Ad B2c自定义用户属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆