gpt4 book ai didi

c# - 批处理: userPrincipalName already exists when trying to add multiple users async in AAD

转载 作者:行者123 更新时间:2023-11-30 16:49:28 30 4
gpt4 key购买 nike

我正在尝试使用 AAD 图表异步添加多个用户,如下所示:

IUser user1 = ...;
IUser user2 = ...;
IUser user3 = ...;
List<Task> addTasks = new List<Task>();
addTasks.Add(activeDirectoryClient.Users.AddUserAsync(user1));
addTasks.Add(activeDirectoryClient.Users.AddUserAsync(user2));
addTasks.Add(activeDirectoryClient.Users.AddUserAsync(user3));
await Task.WhenAll(addTasks);

我收到此错误消息:

{"odata.error":{"code":"Request_BadRequest","message":{"lang":"en","value":"Another object with the same value for property userPrincipalName already exists."},"values":[{"item":"PropertyName","value":"userPrincipalName"},{"item":"PropertyErrorCode","value":"ObjectConflict"}]}}

如果我使用此代码:

IUser user1 = ...;
IUser user2 = ...;
IUser user3 = ...;
List<Task> addTasks = new List<Task>();
await activeDirectoryClient.Users.AddUserAsync(user1);
await activeDirectoryClient.Users.AddUserAsync(user2);
await activeDirectoryClient.Users.AddUserAsync(user3);

这运作良好。

也许我们无法同时在 AAD 中添加多个用户?

编辑这是用户部分:

IUser newStudentUser = new User
{
DisplayName = $"Etudiant de l'école {school}",
UserPrincipalName = $"etudiant-{school}@........fr",
AccountEnabled = true,
MailNickname = $"Etudiant {school}",
UsageLocation = "US",
PasswordProfile = new PasswordProfile
{
Password = "......."
}
};
IUser newTeacherUser = new User
{
DisplayName = $"Professeur de l'école {school}",
UserPrincipalName = $"professeur-{school}@........fr",
AccountEnabled = true,
MailNickname = $"Professeur {school}",
UsageLocation = "US",
PasswordProfile = new PasswordProfile
{
Password = "......."
}
};
IUser newDirectorUser = new User
{
DisplayName = $"Directeur de l'école {school}",
UserPrincipalName = $"directeur-{school}@........fr",
AccountEnabled = true,
MailNickname = $"Directeur {school}",
UsageLocation = "US",
PasswordProfile = new PasswordProfile
{
Password = "......."
}
};

最佳答案

就您而言,您希望批量创建多个用户。

目前,GraphClient确实支持批处理,但有一些限制( Batch processing | Graph API concepts ):

  • 单个批处理最多可以包含五个查询和/或组合的更改集。
  • 一个更改集最多可以包含一个源对象修改以及最多 20 个添加链接和删除链接操作。更改集中的所有操作都必须在单个源实体上进行。

因此,在您的情况下您无法批量创建超过 5 个用户

向 Graph API 客户端添加实体时,您可以选择使用 deferredSave 参数推迟查询的执行。

await activeDirectoryClient.Users.AddUserAsync(user, deferredSave: true);

Graph API 客户端有一个跟踪更改的 DataServiceContextWrapper。它提供了一个SaveChanges(Async)方法。

await activeDirectoryClient.Context.SaveChangesAsync();

调用此方法您可以指定 SaveChangesOptions :

  • 批量:所有待处理的更改都保存在单个批量请求中。
  • BatchWithIndependentOperations:您要使用的 BatchWithIndependentOperations,因为用户的创建应该在单个查询中进行。
  • ContinueOnError:通过向服务器发出多个请求来保存待处理的更改,并在发生错误后继续操作。
  • 无:通过向服务器发送多个请求来保存待处理的更改,但操作会在第一次失败时停止(默认)。
  • 更新补丁:
  • ReplaceOnUpdate:待更新是通过将数据源中实体的所有值替换为更新实体中的值来进行的。

现在您有足够的信息来编写代码来批量创建用户:

// Only 5 users per batch !!!!
var user1 = ...;
var user2 = ...;
var user3 = ...;
await activeDirectoryClient.Users.AddUserAsync(newStudentUser, deferredSave: true);
await activeDirectoryClient.Users.AddUserAsync(newTeacherUser, deferredSave: true);
await activeDirectoryClient.Users.AddUserAsync(newDirectorUser, deferredSave: true);

// In debug mode, you should use the SaveChangesAsync method with the default options
// Becasue the BatchWithIndependentOperations will not throw any exception even if there is a problem while creating the user.
//await activeDirectoryClient.Context.SaveChangesAsync();
await activeDirectoryClient.Context
.SaveChangesAsync(SaveChangesOptions.BatchWithIndependentOperations);

关于c# - 批处理: userPrincipalName already exists when trying to add multiple users async in AAD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36644354/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com