我是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:指定了一个或多个属性值 无效。内部错误
答案 0 :(得分: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();
}
}