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

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

问题描述

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

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

忽略所有其他情况,获得连接的GraphServiceClient等.我正在尝试与以下示例非常相似的代码,没有遇到任何异常(建议一切正常),但是当我再次通过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!

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

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文档

将成员添加到组

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

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

通过Graph API分配用户许可

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

As usual, thanks for any help.

彼得

其他

我还尝试过在图API中四处寻找可能会更新.Members属性/资源而不是组本身的信息:

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()

但是它只能使用GetAync()方法.

已根据答案更新

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.

感谢南宇的回答.

推荐答案

要将用户添加到网上论坛,您可以使用:

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);

获取小组成员:

        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天全站免登陆