C# 上下文已经在跟踪实体 Azure Active Directory 图形 API.添加组成员
本文关键字:Directory Active 图形 API 组成员 添加 Azure 实体 上下文 跟踪 | 更新日期: 2023-09-27 18:31:30
我正在尝试将成员添加到 azure 活动目录组,但它失败了,它显示我以下错误。
上下文已在跟踪实体
我试图为它找到很多,我也看到了这个链接
Azure Active Directory 图形客户端 2.0 - 上下文当前未跟踪实体
http://blogs.msdn.com/b/aadgraphteam/archive/2014/12/12/announcing-azure-ad-graph-api-client-library-2-0.aspx
但我没有得到任何成功,请帮助我。
这是我的代码:
try
{
ActiveDirectoryClient client = ADGraphHelper.GetActiveDirectoryClient();
IGroupFetcher groupFetcher = client.Groups.GetByObjectId(groupId);
Group group = (Group)(await groupFetcher.ExecuteAsync());
string[] userIds = userId.Split(',');
foreach (string id in userIds)
{
if (id != "")
{
IUser user = client.Users.Where(u => u.ObjectId == id).ExecuteAsync().Result.CurrentPage.ToArray().First();
if (user != null)
{
//check wather user aleady present into group or not
IDirectoryObject userExists = group.Members.Where(u => u.ObjectId == id).FirstOrDefault();
if (userExists == null)
{
group.Members.Add(user as DirectoryObject);
}
}
else
throw new Exception("User is null.");
}
}
await group.UpdateAsync();
return Json(new { success = true });
}
catch (Exception ex)
{
ModelState.AddModelError("", "we connot process your request please contact to support for more details.");
// error handling code.
return PartialView();
}
大家好,我发现了现在的问题。 这是旧图形客户端库上的错误。 在图形客户端库 2.0 中解决,因此请使用图形客户端库 2.0 或更高版本。
您可以在以下链接下载最新的图形客户端库:https://www.nuget.org/packages/Microsoft.Azure.ActiveDirectory.GraphClient/