gpt4 book ai didi

c# - MS Graph API C# 将用户添加到组

转载 作者:行者123 更新时间:2023-12-02 02:40:02 24 4
gpt4 key购买 nike

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

Nuget MS Graph API

忽略有关获取连接的GraphServiceClient等的所有其他内容。我正在尝试与下面的示例非常相似的代码,没有收到任何异常(表明一切都很好),但是当我再次获得该组时通过 API,它还没有任何成员!

Quickwatch of returned group object after modification attempt

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

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.us<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="492c3b0924303d2c2728273d672a2624" rel="noreferrer noopener nofollow">[email protected]</a>"
// groupId => the object GUID for the group

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

API doc for 'group'

Adding a member to a group

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

Assign user license via Graph API

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

彼得

附加

我还尝试在图形 API 中查看是否有可能更新 .Members 属性/资源,而不是组本身:

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

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

感谢南宇的解答。

最佳答案

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

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

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

IUserMemberOfCollectionWithReferencesPage memberOfGroups = await graphClient.Users["<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6d19081e19030c0314182d19081e190f0c1e040e5c43020300040e1f021e020b19430e0200" rel="noreferrer noopener nofollow">[email protected]</a>"].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
});
}

}
}

关于c# - MS Graph API C# 将用户添加到组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44115248/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com