【发布时间】:2021-06-29 05:12:48
【问题描述】:
C#中批量添加用户到AAD组或从AAD组中删除用户的代码是什么? (首先找到批量大小,然后添加或删除用户)。任何示例代码都会很棒。
更新:
我添加了以下代码:
private HttpRequestMessage MakeRequest(AzureADUser user, Guid targetGroup)
{
return new HttpRequestMessage(HttpMethod.Patch, $"https://graph.microsoft.com/v1.0/groups/{targetGroup}")
{
Content = new StringContent(MakeAddRequestBody(user), System.Text.Encoding.UTF8, "application/json"),
};
}
private static string MakeAddRequestBody(AzureADUser user)
{
JObject body = new JObject
{
["members@odata.bind"] = JArray.FromObject($"https://graph.microsoft.com/v1.0/users/{user.ObjectId}")
};
return body.ToString(Newtonsoft.Json.Formatting.None);
}
public async Task AddUsersToGroup1(IEnumerable<AzureADUser> users, AzureADGroup targetGroup)
{
try
{
var batches = GetBatchRequest(users, targetGroup.ObjectId);
foreach (var batchRequestContent in batches)
{
var response = await _graphServiceClient
.Batch
.Request()
.WithMaxRetry(10)
.PostAsync(batchRequestContent);
var responses = await response.GetResponsesAsync();
}
}
catch (Exception ex)
{
}
}
在运行时,我得到以下异常:对象序列化为字符串。需要 JArray 实例。我错过了什么?此外,一旦我收到响应,我需要检查所有响应是否返回“OK”响应或不类似于:
return responses.Any(x => x == ResponseCode.Error) ? ResponseCode.Error : ResponseCode.Ok;
我该怎么做?
【问题讨论】:
标签: c# azure-active-directory microsoft-graph-api