[英]Azure Active Directory Api - Add user To Group Fails
I'm trying to add user already exists on the azure active directory to a group already exists there too , via Azure AD Graph API: 我正在尝试通过Azure AD Graph API将azure活动目录上已经存在的用户添加到那里已经存在的组中:
IUser userToBeAdded1 = activeDirectoryClient.Users.Where(user => user.ObjectId == "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx").ExecuteSingleAsync().Result;
Group retrievedGroup = new Group();
string searchString = "Development";
List<IGroup> foundGroups = null;
foundGroups = activeDirectoryClient.Groups.Where(group => group.DisplayName.StartsWith(searchString)).ExecuteAsync().Result.CurrentPage.ToList();
if (foundGroups != null && foundGroups.Count > 0)
{
retrievedGroup = foundGroups.First() as Group;
if (retrievedGroup.ObjectId != null)
{
try
{
activeDirectoryClient.Context.AddLink(retrievedGroup, "members", userToBeAdded1);
activeDirectoryClient.Context.SaveChanges();
}
catch (Exception ex)
{
Console.WriteLine("\nError assigning member to group. {0} {1}",
ex.Message, ex.InnerException != null ? ex.InnerException.Message : "");
}
}
}
and each time i get the same error : "The context is not currently tracking the entity." 并且每次我遇到相同的错误:“上下文当前未跟踪该实体。”
at this line : 在这一行:
activeDirectoryClient.Context.AddLink(retrievedGroup, "members", userToBeAdded1);
This issue is now fixed in the latest version of the Graph client library. 现在已在最新版本的Graph客户端库中修复了此问题。 Please see the answer here for details: Azure Active Directory Graph Client 2.0 - Context is not currently tracking the entity 有关详细信息,请参见此处的答案: Azure Active Directory Graph Client 2.0-上下文当前未跟踪实体
Hope this helps, 希望这可以帮助,
Try this (not tested): 试试看(未测试):
var userToBeAdded1 = activeDirectoryClient.Users
.FirstOrDefaultAsync(user => user.ObjectId == "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
var searchString = "Development";
var foundGroup = activeDirectoryClient.Groups
.FirstOrDefaultAsync(group => group.DisplayName.StartsWith(searchString));
if (foundGroup != null && foundGroup.ObjectId != null)
{
try
{
activeDirectoryClient.Context.AddLink(retrievedGroup, "members", userToBeAdded1);
activeDirectoryClient.Context.SaveChanges();
}
catch (Exception ex)
{
Console.WriteLine("\nError assigning member to group. {0} {1}", ex.Message, ex.InnerException != null ? ex.InnerException.Message : "");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.