在 Net Core 中使用 Microsoft Graph Api 将用户添加为组成员
Add users as group members with Microsoft Graph Api in Net Core
在我的项目中,我在我的 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)
似乎不起作用,因为我从图表 Api 返回格式不正确的 OData 字段:Invalid URL format specified in @odata.bind for members
我有:
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 for members
。所以我修改了我的代码如下:
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
权限)
希望对你有帮助~
在我的项目中,我在我的 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)
似乎不起作用,因为我从图表 Api 返回格式不正确的 OData 字段:Invalid URL format specified in @odata.bind for members
我有:
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 for members
。所以我修改了我的代码如下:
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
权限)
希望对你有帮助~