我尝试通过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 : "");
}
}
}
每次我都得到同样的错误: &#34;上下文当前没有跟踪实体。&#34;
在这一行:
activeDirectoryClient.Context.AddLink(retrievedGroup, "members", userToBeAdded1);
答案 0 :(得分:1)
此问题现已在最新版本的Graph客户端库中修复。有关详细信息,请参阅此处的答案:Azure Active Directory Graph Client 2.0 - Context is not currently tracking the entity
希望这有帮助,
答案 1 :(得分:-1)
试试这个(未经测试):
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 : "");
}
}