【问题标题】:How to update azure ad b2c custom user attribute using graph api如何使用图形 api 更新 azure ad b2c 自定义用户属性
【发布时间】:2020-09-09 16:22:23
【问题描述】:

我是 Azure B2C 的新手。我针对用户对象创建了自定义属性 extension_role。我想使用图形 api 更新此属性。我尝试了以下代码

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

这是更新自定义属性的正确方法吗?在执行时我也遇到了错误

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

【问题讨论】:

    标签: azure microsoft-graph-api azure-ad-b2c azure-ad-graph-api azure-ad-b2c-custom-policy


    【解决方案1】:

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

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

    【讨论】:

    • 结果是 Microsoft.Graph.User。更新时发生同样的错误
    • 用户是否拥有您要更新的自定义属性
    • 我调用了用户列表,然后它像 extension_50c0962467e94558913d853598913dsd_role 这样返回
    • 50c0962467e94558913d853598913dsd 此值为 b2c-extensions-app 的应用程序 ID。不要修改。由 AADB2C 用于存储用户数据。
    • 如何检查这些值?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-05
    • 2018-03-23
    相关资源
    最近更新 更多