【问题标题】:How to create an appRoleAssignment via Microsoft Graph?如何通过 Microsoft Graph 创建 appRoleAssignment?
【发布时间】:2019-11-22 07:34:51
【问题描述】:

根据this documentation,您应该能够通过 Microsoft Graph 创建 appRoleAssignment,但这不起作用。在GitHub issue 中,我被指示在此处创建问题。我们已将大部分代码从 Azure Graph API 迁移到 Microsoft Graph,这是最后一个缺失的部分。

【问题讨论】:

    标签: microsoft-graph-api


    【解决方案1】:

    这终于对我有用了!

    可能有更优化的方式来发布 JSON,但我必须深入了解基础知识以确保没有任何东西导致这在幕后失败。

            const string ROLE_ASSIGNMENT_FORMATTER = "https://graph.microsoft.com/beta/servicePrincipals/{0}/appRoleAssignments";
    
            public static async Task AddApplicationUsers(string enterpriseAppId, string userId, string roleId)
            {
                HttpClient client = new HttpClient();
                string url = string.Format(ROLE_ASSIGNMENT_FORMATTER, enterpriseAppId);
    
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await GetAccessToken());
    
                var roleAssignment = new
                {
                    appRoleId = roleId,
                    principalId = userId,
                    resourceId = enterpriseAppId
                };
    
    
                var content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(roleAssignment), Encoding.UTF8, "application/json");
                content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                var response  = await client.PostAsync(url, content);
    
                if (response.IsSuccessStatusCode)
                {
                    return ;
                }
                else
                {
                    throw new HttpRequestException(response.ReasonPhrase);
                }
            }
    

    【讨论】:

    • 我很想听听您是否知道为什么 SDK 不适合您。我在这里打开了一个问题github.com/microsoftgraph/msgraph-sdk-dotnet/issues/537 来跟踪它。
    • @DarrelMiller 我想我在 .NET MS Graph SDK 中找不到这种方法。我检查了预发布版本,我猜我使用的是最新的 nuget 版本,所以最终不得不根据文档和示例自行完成
    • 啊,我明白了。 Beta API 有不同的 SDK。 nuget 包的链接在这里docs.microsoft.com/en-us/graph/sdks/…
    • 哈!这解释了很多,谢谢!我不得不以这种方式编写很多方法,因为我找不到它们。我知道我应该更多地寻找 SDK,但我认为直接 REST API 反正也不错:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-03
    • 2021-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多