1
A
回答
2
按照documentation,图形API支持批量处理。 Microsoft Azure Active Directory Client支持批处理。
你可以找到很多的样品使用Azure的AD图形API在这里:
尤其是你对大多数的行动,你可以与执行全样本此处的图表API:
- Call the Azure AD Graph API from a native client(在program.cs文件中搜索“批处理操作”)。
不幸的是批处理工作不导航性能,或者至少我没有找到一种方法,使其工作...
因此,让我们来看看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; }
}
相关问题
- 1. Azure AD Graph API访问,不使用客户端ID和密钥
- 2. 从Javascript客户端批量处理ServiceStack请求客户端
- 3. 使用Azure AD Graph客户端API更改用户密码的权限问题
- 4. Microsoft Graph Azure AD用户不同步
- 5. 使用Microsoft Graph API在Azure AD中创建访客用户
- 6. 使用Microsoft Graph .NET客户端库指定管理器
- 7. severside处理vs客户端处理+ ajax?
- 8. 处理多客户端流?
- 9. 处理多个客户端
- 10. Azure AppFabric客户端
- 11. Azure AD OAuth客户端凭据流访问控制
- 12. Azure AD - 禁用客户端的应用程序
- 13. 针对桌面客户端的Azure AD B2C本机应用Redirect_uri
- 14. 验证到Azure的AD客户端应用程序
- 15. 邮差客户端凭证与Azure的AD保护的ressource
- 16. AD用户文件夹清理批处理脚本
- 17. 的Windows Azure:存储客户端异常未处理
- 18. IdentityServer4在Azure中处理什么样的“客户端请求”?
- 19. MarkLogic 9 Java客户端与批处理器中的错误
- 20. 在客户端显示服务器批处理进度
- 21. 执行批处理文件在客户端页面
- 22. Azure AD租户的Azure AD域服务?
- 23. Azure AD B2C,Azure AD租户和ADFS
- 24. nservicebus可以处理数据库客户端到msmq后端?
- 25. 客户端身份验证 - 处理客户端证书
- 26. 使用代理服务器上载Azure存储客户端库
- 27. 服务器端处理或客户端处理?
- 28. 客户端处理vs服务器端处理,速度快吗?
- 29. 是否可以使用Azure Ad Graph API处理一个驱动器文件?
- 30. Facebook Graph API批处理 - 迭代分页
嗨,托马斯,我做了同样的事情,但我想批量处理20条记录(将用户连接到组)的相同操作,以及即时通讯与其挣扎的地方。 – user2030504
是否要将20个用户添加到同一组?你想解释一下你想要做什么吗? – Thomas
我想将超过100个用户链接到不同的组。但由于性能问题,我想利用批量操作。 – user2030504