簡體   English   中英

在 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 表示,如下所示( 文檔):

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 放入字典 object 的正確方法是什么?

問題是由代碼中的轉義字符\"引起的,我測試了文檔中的相同代碼,也看到了相同的錯誤消息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權限)

希望對你有幫助~

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM