MS Graph API C# 将用户添加到组 [英] MS Graph API C# Add user to group

查看:42
本文介绍了MS Graph API C# 将用户添加到组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究如何使用 Microsoft Graph API(nuget 上提供的 dotnet/C# 库)从 Azure AD 组中添加(然后删除)用户.

有趣的是(顺便说一句),当我在 user 对象上请求 memberOf 属性并告诉它扩展它时,它仍然返回 null.

var user = await client.Users[userPrincipalName].要求().Select("id,memberOf").Expand("memberOf").GetAsync();var group = await client.Groups[groupId].要求().Select("成员").Expand("成员").GetAsync();group.Members.Add(user);等待 client.Groups[groupId].Request().UpdateAsync(group);//userPrincipalName =>test.user@mytenant.com"//groupId =>组的对象 GUID

有谁知道我在这里做错了什么?我用来编写此代码的文档基于指向以下文档的链接:

组"的 API 文档

将成员添加到组

此外,我尝试根据此处建议的解决方案设置用户许可的方法:

通过图谱 API 分配用户许可

像往常一样,感谢您的帮助.

彼得

附加

我还尝试在图形 API 中寻找可能更新 .Members 属性/资源而不是组本身:

await client.Groups[groupId].Members.Request().//<-- 只有 GetAsync()

但它只有 GetAync() 方法可用.

根据答案更新

var usersGroups = await client.Users[userPrincipalName].MemberOf.Request().GetAsync();if (!usersGroups.Any(g => g is Group && g.Id == groupId)){//用户不是组的成员,添加他们.var user = await client.Users[userPrincipalName].Request().Select("id").GetAsync();等待 client.Groups[groupId].Members.References.Request().AddAsync(user);}

我根据答案添加了上面的代码片段,因为我认为它简洁地回答了有关添加成员的问题.

感谢南宇的回答.

解决方案

要将用户添加到 Group ,您可以使用:

User userToAdd = await graphClient.Users["objectID"].Request().GetAsync();等待 graphClient.Groups["groupObjectID"].Members.References.Request().AddAsync(userToAdd);

获取群组成员:

 Listitems = new List();//获取组成员.IGroupMembersCollectionWithReferencesPage members = await graphClient.Groups[id].Members.Request().GetAsync();如果(成员?.计数> 0){foreach(成员中的用户用户){//获取成员属性.items.Add(新的结果项{属性 = 新字典<字符串,对象>{{ Resource.Prop_Upn, user.UserPrincipalName },{ Resource.Prop_Id, user.Id }}});}}

获取当前用户是其直接成员的组,您可以尝试:

IUserMemberOfCollectionWithReferencesPage memberOfGroups = await graphClient.Users["testnanyu@testbasic1.onmicrosoft.com"].MemberOf.Request().GetAsync();如果 (memberOfGroups?.Count > 0){foreach (var directoryObject in memberOfGroups){//我们只需要组,所以忽略 DirectoryRole 对象.如果(目录对象是组){Group group = directoryObject as Group;items.Add(新的结果项{Display = group.DisplayName,Id = group.Id});}}}

I've been investigating how to add (and later remove) a user from an Azure AD group using the Microsoft Graph API (the dotnet/C# library available on nuget).

Nuget MS Graph API

Ignoring all else around getting a connected GraphServiceClient etc. I'm trying code very similar to the sample below, not getting any exception (suggesting things are fine) but when I get the group once again via the API, it's not got any members still!

Interestingly (as an aside), when I ask for memberOf property on the user object and tell it to expand it, it comes back null still.

var user = await client.Users[userPrincipalName]
             .Request()
               .Select("id,memberOf")
                 .Expand("memberOf")
                   .GetAsync();

var group = await client.Groups[groupId]
              .Request()
                .Select("members")
                  .Expand("members")
                    .GetAsync();

group.Members.Add(user);

await client.Groups[groupId].Request().UpdateAsync(group);

// userPrincipalName => "test.user@mytenant.com"
// groupId => the object GUID for the group

Does anyone know what I'm doing wrong here? The docs I used to come up with this code were based on the links to the following documents:

API doc for 'group'

Adding a member to a group

Also, I tried to style the approach on the solution suggested here to setting licenses for users:

Assign user license via Graph API

As usual, thanks for any help.

Peter

Additional

I've also tried poking around in the graph API looking at potentially updating the .Members property/resource rather than the group itself:

await client.Groups[groupId].Members.Request(). // <-- Only has GetAsync()

But it only has the GetAync() method available to it.

Updated based on answer

var usersGroups = await client.Users[userPrincipalName].MemberOf.Request().GetAsync();

if (!usersGroups.Any(g => g is Group && g.Id == groupId))
{
    // User is not a member of the group, add them.
    var user = await client.Users[userPrincipalName].Request().Select("id").GetAsync();
    await client.Groups[groupId].Members.References.Request().AddAsync(user);
}

I've added the code snippet above based on the answer, as I think it succinctly answers the issue regarding adding members.

Thanks to Nan Yu for the answer.

解决方案

To add user to Group ,you could use :

User userToAdd = await graphClient.Users["objectID"].Request().GetAsync(); 
await graphClient.Groups["groupObjectID"].Members.References.Request().AddAsync(userToAdd);

To get members of a group :

        List<ResultsItem> items = new List<ResultsItem>();

        // Get group members. 
        IGroupMembersCollectionWithReferencesPage members = await graphClient.Groups[id].Members.Request().GetAsync();

        if (members?.Count > 0)
        {
            foreach (User user in members)
            {
                // Get member properties.
                items.Add(new ResultsItem
                {
                    Properties = new Dictionary<string, object>
                    {
                        { Resource.Prop_Upn, user.UserPrincipalName },
                        { Resource.Prop_Id, user.Id }
                    }
                });
            }
        }

Get groups the current user is a direct member of ,you could try :

IUserMemberOfCollectionWithReferencesPage memberOfGroups = await graphClient.Users["testnanyu@testbasic1.onmicrosoft.com"].MemberOf.Request().GetAsync();

            if (memberOfGroups?.Count > 0)
            {
                foreach (var directoryObject in memberOfGroups)
                {

                    // We only want groups, so ignore DirectoryRole objects.
                    if (directoryObject is Group)
                    {
                        Group group = directoryObject as Group;
                        items.Add(new ResultsItem
                        {
                            Display = group.DisplayName,
                            Id = group.Id
                        });
                    }

                }
            }

这篇关于MS Graph API C# 将用户添加到组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆