【问题标题】:Add Members to group in azure active directory using Graph API gives URl invalid error使用 Graph API 将成员添加到 azure Active Directory 中的组会导致 URl 无效错误
【发布时间】:2020-06-29 08:31:51
【问题描述】:

我正在尝试使用 C# 中的 Graph API 将成员添加到 Azure Active Directory 中的组,但是当我运行代码时出现以下错误:

ServiceException:代码:BadRequest
消息:无效的 URL 格式 在@odata.bind 中为成员指定

下面是我的代码:

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

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

【问题讨论】:

    标签: c# asp.net asp.net-core azure-active-directory azure-ad-graph-api


    【解决方案1】:

    您可以使用字符串数组,而不是将成员作为字符串提供(并通过预期的 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 可以处理之后的序列化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多