简体   繁体   English

Azure Active Directory Api-将用户添加到组失败

[英]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.

相关问题 使用图形api在Azure Active目录组中搜索用户 - Search for user in Azure Active directory group using graph api 如何使用图形API在Azure活动目录中添加外部用户 - How to add the external user in azure active directory using graph api C#将活动目录用户添加到组 - C# add active directory user to group 使用图表 API 将成员添加到 azure 活动目录中的组会给出 URl 无效错误 - Add Members to group in azure active directory using Graph API gives URl invalid error C#上下文已经在跟踪实体,蔚蓝的活动目录图api。 添加组成员 - C# context is already tracking the entity, azure active directory graph api. add group member 使用图API API在Azure Active Directory中创建应用程序失败 - Create application in Azure Active Directory using graph API fails MVC5如何遍历Azure活动目录用户组 - MVC5 how to iterate through an azure active directory user group 单个用户的Azure Active Directory组成员身份检查 - Azure Active Directory Group Membership check for Individual User 如何获取Azure Active Directory角色或用户组 - How to get Azure Active Directory role or user group 如何在Azure活动目录中获取用户的安全组名称 - how to get the security group names of a user in azure active directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM