使用 Graph API 将成员添加到 azure 活动目录中的组会出现 URl 无效错误

Add Members to group in azure active directory using Graph API gives URl invalid error

我正在尝试使用 C# 中的图形 API 将成员添加到 Azure 活动目录中的组,但是当我 运行 代码时,我收到以下错误:

ServiceException: Code: BadRequest
Message: Invalid URL format specified in @odata.bind for members

下面是我的代码:

[AuthorizeForScopes(Scopes = new[] { Constants.GroupMemberReadWriteAll, Constants.DirectoryReadAsUser })]
public async Task<IActionResult> AddMembersToGroup()
{
    MSGraphs.GraphServiceClient graphClient = GetGraphServiceClient(new[] { Constants.GroupMemberReadWriteAll, Constants.DirectoryReadAsUser });

            var group = new MSGraphs.Group
            {
                AdditionalData = new Dictionary<string, object>()
                {
                    {"members@odata.bind", "[\"https://graph.microsoft.com/v1.0/directoryObjects/c6e5c868-e2bd-46b0-b5f3-c39b5dfe42dc\",\"https://graph.microsoft.com/v1.0/directoryObjects/b2fc878b-a455-4315-b9e1-8594cbad3404\",\"https://graph.microsoft.com/v1.0/directoryObjects/79f06743-3bee-418e-8f4e-b381ebdfafc2\"]"}
                }
            };

            await graphClient.Groups["{29f7e645-8de1-45fc-80a5-e76e0fa51340}"]
                .Request()
                .UpdateAsync(group);

            return Ok();
        }

如果我在这里遗漏了什么,请告诉我。

您可以使用字符串数组,而不是将成员作为字符串提供(并通过预期的 JSON 序列化):

var group = new MSGraphs.Group
  {
    AdditionalData = new Dictionary<string, object>()
      {
         { 
            "members@odata.bind", 
            new string[] 
            { 
              "https://graph.microsoft.com/v1.0/directoryObjects/c6e5c868-e2bd-46b0-b5f3-c39b5dfe42dc",
              "https://graph.microsoft.com/v1.0/directoryObjects/b2fc878b-a455-4315-b9e1-8594cbad3404",
              "https://graph.microsoft.com/v1.0/directoryObjects/79f06743-3bee-418e-8f4e-b381ebdfafc2" 
            }
         }
     }
  };

这样,.NET 可以在之后处理序列化。