回答

2

按照documentation,图形API支持批量处理。 Microsoft Azure Active Directory Client支持批处理。

你可以找到很多的样品使用Azure的AD图形API在这里:

尤其是你对大多数的行动,你可以与执行全样本此处的图表API:

不幸的是批处理工作不导航性能,或者至少我没有找到一种方法,使其工作...

因此,让我们来看看documentation。为OData批量请求

图形API支持:

  • 查询是单个查询或功能调用。
  • 更改集是一组一个或多个插入,更新或删除操作,操作调用或服务调用。
  • 批处理是操作的容器,包括一个或多个更改集和查询操作。

的图形API支持由 OData规范中定义的功能的子集:

  • 单个批次可以包含最多五个查询和/或改变集组合。
  • 变更集最多可包含一个源对象修改,最多可包含20个组合的添加 - 链接和删除链接操作 。变更集中的所有操作必须位于单个源 实体上。

这里是批处理请求语法:

https://graph.windows.net/TenantName/$batch?api-version=1.6 

批处理请求被发送到所述服务器与单个POST指令。

有效负载是一个多部分MIME消息包含批处理及其组成查询和更改集。有效载荷包括两种类型的MIME边界的:

  • 批次边界每个查询和/或改变在批处理设置分离。
  • A 更改集边界将变更集中的各个操作分开。

变更集中的单个请求与自己调用该操作时发出的请求完全相同。here is a sample request

你可以在这里找到完整的示例代码:azure-active-directory-batchprocessing 所以基本上,你需要获得身份验证令牌:

var authority = "https://login.microsoftonline.com/mytenantName.onmicrosoft.com"; 
var resource = "https://graph.windows.net/"; 
var clientId = "ClientId of the application in the Azure Active Directory"; 
var clientSecret = "ClientSecret of the application in the Azure Active Directory"; 

var token = new AuthenticationContext(authority, false).AcquireToken(resource, 
     new ClientCredential(clientId, clientSecret)).AccessToken; 

在你的问题,你想添加成员到组(see Graph API Documentation on groups):

// Get the objectId of the group 
var groupId = ... 

// Get the member ids you'd like to add to the group 
var memberIds = ... 

这里是将成员添加到组的代码:

private static async Task AddMemberToGroup(string token, string groupId, IList<string> memberIds) 
{ 
    if (memberIds.Count > 100) 
    { 
     // A batch can contain up to 5 changesets. Each changeset can contain up to 20 operations. 
     throw new InvalidOperationException("Cannot send more than 100 operation in an batch"); 
    } 

    var batch = new BatchRequest("https://graph.windows.net/MyTenantName.onmicrosoft.com"); 

    // A changeset can contain up to 20 operations 
    var takeCount = 20; 
    var skipCount = 0; 
    var take = memberIds.Skip(skipCount).Take(takeCount).ToList(); 
    while (take.Count > 0) 
    { 
     AddChangeset(batch, groupId, take); 
     skipCount += takeCount; 
     take = memberIds.Skip(skipCount).Take(takeCount).ToList(); 
    } 

    using (var client = new HttpClient()) 
    { 
     client.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}"); 
     var response = await client.SendAsync(batch.Request); 
    } 
} 

private static void AddChangeset(BatchRequest batch, string groupId, IEnumerable<string> memberIds) 
{ 
    var changeset = batch.AddChangeSet(); 
    foreach (var memberId in memberIds) 
    { 
     // Create the HttpRequest to add a member to a group 
     var request = AddMemberToGroupRequest(groupId, memberId); 

     // Add the operation to the changeset 
     changeset.AddOperation(request); 
    } 
} 

private static HttpRequestMessage AddMemberToGroupRequest(string groupId, string memberId) 
{ 
    // Create a request to add a member to a group 
    var request = new HttpRequestMessage(HttpMethod.Post, 
     $"https://graph.windows.net/MyTenantName.onmicrosoft.com/groups/{groupId}/$links/members?api-version=1.6"); 

    // Create the body of the request 
    var jsonBody = 
     JsonConvert.SerializeObject(new DirectoryObject($"https://graph.windows.net/MyTenantName.onmicrosoft.com/directoryObjects/{memberId}")); 

    // Set the content 
    request.Content = new StringContent(jsonBody, Encoding.UTF8, "application/json"); 

    // Return the request 
    return request; 
} 

public class BatchRequest 
{ 
    private readonly MultipartContent _batchContent; 

    public BatchRequest(string tenantUrl) 
    { 
     // Create the batch request 
     Request = new HttpRequestMessage(HttpMethod.Post, 
      $"{tenantUrl}/$batch?api-version=1.6"); 

     // Initializes the batch content 
     _batchContent = new MultipartContent("mixed", "batch_" + Guid.NewGuid()); 
     Request.Content = _batchContent; 
    } 

    public HttpRequestMessage Request { get; } 

    public ChangeSet AddChangeSet() 
    { 
     // Create a new changeset 
     var changeSet = new ChangeSet(); 

     // Add the content of the changeset to the batch 
     _batchContent.Add(changeSet.Content); 

     // return the changeset 
     return changeSet; 
    } 

    public HttpMessageContent CreateOperation(HttpRequestMessage request) 
    { 
     var content = new HttpMessageContent(request); 
     content.Headers.ContentType = new MediaTypeHeaderValue("application/http"); 
     content.Headers.Add("Content-Transfer-Encoding", "binary"); 
     return content; 
    } 
} 

public class ChangeSet 
{ 
    public ChangeSet() 
    { 
     Content = new MultipartContent("mixed", "changeset_" + Guid.NewGuid()); 
    } 

    public MultipartContent Content { get; } 

    public void AddOperation(HttpRequestMessage request) 
    { 
     var operationContent = new HttpMessageContent(request); 
     operationContent.Headers.ContentType = new MediaTypeHeaderValue("application/http"); 
     operationContent.Headers.Add("Content-Transfer-Encoding", "binary"); 
     Content.Add(operationContent); 
    } 
} 

public class DirectoryObject 
{ 
    public DirectoryObject(string url) 
    { 
     this.url = url; 
    } 
     public string url { get; } 
} 
+0

嗨,托马斯,我做了同样的事情,但我想批量处理20条记录(将用户连接到组)的相同操作,以及即时通讯与其挣扎的地方。 – user2030504

+0

是否要将20个用户添加到同一组?你想解释一下你想要做什么吗? – Thomas

+0

我想将超过100个用户链接到不同的组。但由于性能问题,我想利用批量操作。 – user2030504

相关问题