0

我在 C# 中添加了以下代码,将用户批量添加到 AAD 组:

        public IEnumerable<BatchRequestContent> FindBatches(IEnumerable<AzureADUser> users, AzureADGroup targetGroup)
        {
            var batches = GetBatchRequest(users, targetGroup);
            await AddUsersToGroup(batches);
        }        

        private IEnumerable<BatchRequestContent> GetBatchRequest(IEnumerable<AzureADUser> users, AzureADGroup targetGroup)
        {
            var batches = new List<BatchRequestContent>();
            int maxNoBatchItems = 20;

            var batchRequestContent = new BatchRequestContent();
            int requestId = 1;

            foreach (var user in users)
            {
                JObject body = new JObject
                {
                    ["members@odata.bind"] = $"https://graph.microsoft.com/v1.0/users/{user.ObjectId}"
                };

                var httpRequestMessage = new HttpRequestMessage(HttpMethod.Patch, $"https://graph.microsoft.com/v1.0/groups/{targetGroup.ObjectId}");
                httpRequestMessage.Content = new StringContent(body.ToString(), Encoding.UTF8, "application/json");

                batchRequestContent.AddBatchRequestStep(new BatchRequestStep(requestId.ToString(), httpRequestMessage));

                if (batchRequestContent.BatchRequestSteps.Count() % maxNoBatchItems == 0)
                {
                    batches.Add(batchRequestContent);
                    batchRequestContent = new BatchRequestContent();
                }

                requestId++;
            }

            if (batchRequestContent.BatchRequestSteps.Count < maxNoBatchItems)
            {
                batches.Add(batchRequestContent);
            }

            return batches;
        }

        public async Task AddUsersToGroup(IEnumerable<BatchRequestContent> batches)
        {
            try
            {
                foreach (var batchRequestContent in batches)
                {
                    var response = await _graphServiceClient
                        .Batch
                        .Request()
                        .WithMaxRetry(10)
                        .PostAsync(batchRequestContent);

                    var responses = await response.GetResponsesAsync();

                    foreach (string key in responses.Keys)
                    {
                      HttpResponseMessage httpResponse = await response.GetResponseByIdAsync(key);
                      var responseContent = await httpResponse.Content.ReadAsStringAsync();                      
                      Console.WriteLine($"Response code: {responses[key].StatusCode}-{responses[key].ReasonPhrase}");
                }
                }
            }
            catch (Exception ex)
            {                
            }
        }

当我运行此代码时,我从以下行中看到“错误请求”:

var responses = await response.GetResponsesAsync();

在此处输入图像描述

Response status code does not indicate success: 400 (Bad Request).

在此处输入图像描述

在 Line 上设置断点:

 var responseContent = await httpResponse.Content.ReadAsStringAsync();       

我看到以下错误:

{ "error": { "code": "Request_BadRequest", "message": "资源集引用导航属性 'members' 有一个属性注释 'odata.bind' 带有字符串值。资源集引用导航属性只能有带有数组值的属性注释 'odata.bind'。"} }

我错过了什么?

4

0 回答 0