【问题标题】:Add/remove users to/from AAD group in batches批量向/从 AAD 组添加/删除用户
【发布时间】: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


    【解决方案1】:

    将用户批量添加到 AAD 组中:

    GraphServiceClient graphClient = new GraphServiceClient(authProvider);
    
    var additionalData = new Dictionary<string, object>()
        {
            {"members@odata.bind", new List<string>()}
        };
    (additionalData["members@odata.bind"] as List<string>).Add("https://graph.microsoft.com/v1.0/users/{id}"");
    (additionalData["members@odata.bind"] as List<string>).Add("https://graph.microsoft.com/v1.0/users/{id}"");
    
    var group = new Group
    {
        AdditionalData = additionalData
    };
    
    await graphClient.Groups["{group-id}"]
       .Request()
       .UpdateAsync(group);  
    

    没有可用于从 AAD 组中批量删除用户的端点。但是有一个batch endpoint 在一个 HTTP 调用中组合了多个请求。好像有20个的限制,所以不能一次删除太多用户。

    这里是一个例子,从AAD组中批量删除用户(参考here):

    GraphServiceClient graphClient = new GraphServiceClient(authProvider);
    var removeUserRequest1 = graphClient.Groups["{group-id}"].Members["{id}"].Reference.Request().GetHttpRequestMessage();
    var removeUserRequest2 = graphClient.Groups["{group-id}"].Members["{id}"].Reference.Request().GetHttpRequestMessage();
    
    removeUserRequest1.Method = HttpMethod.Delete;
    removeUserRequest2.Method = HttpMethod.Delete;
    
    var batchRequestContent = new BatchRequestContent();
    
    batchRequestContent.AddBatchRequestStep(removeUserRequest1);
    batchRequestContent.AddBatchRequestStep(removeUserRequest2);
    
    await graphClient.Batch.Request().PostAsync(batchRequestContent);
    

    【讨论】:

    • 谢谢!假设我也需要添加 50 万用户。我可以使用相同的代码吗?或者你能提供相同的代码吗?谢谢!
    • 另外,有没有办法先计算批量大小,然后添加/删除用户?请告诉我。
    • 您能否检查更新部分并回答相同的问题?谢谢!
    • @user989988 我认为您不能使用此代码添加 50 万用户。请使用 csv 文件在门户上创建用户。
    • @user989988 但是 500K 太大了。正如我之前建议的那样,您不能将 50 万用户添加到租户中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-05
    • 2016-05-15
    • 1970-01-01
    • 2015-08-08
    • 2017-10-27
    • 2022-08-02
    相关资源
    最近更新 更多