gpt4 book ai didi

c# - 在 Net Core 中使用 Microsoft Graph Api 将用户添加为组成员

转载 作者:行者123 更新时间:2023-12-05 08:38:34 31 4
gpt4 key购买 nike

在我的项目中,我在我的 Net Core 3.0 项目中使用 Microsoft Graph Api 连接到 Azure Intune 并添加组和用户。添加带有成员的组时,图形 API 需要用户的 json 表示,如下所示 (documentation):

var group = new Group
{
// other properties omitted
AdditionalData = new Dictionary<string, object>()
{
{"owners@odata.bind", "[\"https://graph.microsoft.com/v1.0/users/26be1845-4119-4801-a799-aea79d09f1a2\"]"},
{"members@odata.bind", "[\"https://graph.microsoft.com/v1.0/users/ff7cb387-6688-423c-8188-3da9532a73cc\",\"https://graph.microsoft.com/v1.0/users/69456242-0067-49d3-ba96-9de6f2728e14\"]"}
}
};

简要编辑:我从文档中尝试了上面的代码,将 Guids 替换为我要添加的用户,但这也不起作用,给我同样的错误消息。结束编辑。

如何在字典中动态添加成员,比如从一组用户 ID 中添加?他们似乎使用转义字符,并且使用 JsonConvert.SerializeObject(arrayObjectWithIds) 似乎不起作用,因为我从 Graph Api 返回格式不正确的 OData 字段:指定的 URL 格式无效在 @odata.bind 中为成员

我有什么:

string[] memberIds = new string[] { "https://graph.microsoft.com/v1.0/users/123", "https://graph.microsoft.com/v1.0/users/456", "https://graph.microsoft.com/v1.0/users/789" };
string json = JsonConvert.SerializeObject(memberIds);

newGroup.AdditionalData = new Dictionary<string, object>()
{
{"members@odata.bind", json }
};

// Send it off and get Invalid URL format specified in @odata.bind for members error

这是我的 json,因为它当前附加到字典:

["https://graph.microsoft.com/v1.0/users/123\"","https://graph.microsoft.com/v1.0/users/456\"","https://graph.microsoft.com/v1.0/users/789\""]

将成员 uri 放入字典对象的正确方法是什么?

最佳答案

问题是由代码中的转义字符\"引起的,我从文档中测试了相同的代码,也看到了相同的错误信息Invalid URL format specified in @odata。绑定(bind)成员。所以我修改了我的代码如下:

var additionalData = new Dictionary<string, object>()
{
{"owners@odata.bind", new List<string>()},
{"members@odata.bind", new List<string>()}
};
(additionalData["members@odata.bind"] as List<string>).Add("https://graph.microsoft.com/v1.0/users/xxxxx");
(additionalData["owners@odata.bind"] as List<string>).Add("https://graph.microsoft.com/v1.0/users/xxxxx");

var group = new Group
{
Description = "Group with designated owner and members",
DisplayName = "huryNewGroup",
GroupTypes = new List<String>()
{
"Unified"
},
MailEnabled = true,
MailNickname = "operations2019",
SecurityEnabled = false,
AdditionalData = additionalData
};

运行代码,我成功创建了包含成员的组。

顺便说一句,我们可能会遇到权限问题。起初我只为应用程序添加了权限 Group.ReadWrite.All 但是当我运行代码时它显示我没有权限。然后我添加了其他权限Directory.ReadWrite.All, Directory.AccessAsUser.All,它工作正常。(据我所知,Group有一些小问题> 权限,所以你最好添加其他 Directory 权限)

希望对你有帮助~

关于c# - 在 Net Core 中使用 Microsoft Graph Api 将用户添加为组成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62475635/

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