Azure AD Graph - 使用应用程序凭据流创建 AppRole

Azure AD Graph - AppRole Creation using Application Credential Flow

我正在使用 Azure AD Graph 在 Azure 应用程序中创建一个新角色 API。我正在做的是使用以下代码从 azure 获取访问令牌:

ClientCredential clientCredential = new ClientCredential(clientId, clientSecret);
AuthenticationContext authenticationContext = new AuthenticationContext(aadInstance + tenantID);
AuthenticationResult authenticationResult = await authenticationContext.AcquireTokenAsync(graphResourceID, clientCredential);
return authenticationResult.AccessToken;

并使用以下代码创建角色:

//Fetch application Data from azure AD
IApplication application = await activeDirectoryClient.Applications.GetByObjectId(RoleModel.ApplicationID).ExecuteAsync();
AppRole NewRole = new AppRole
{
    Id = CurrentRoleID,
    IsEnabled = true,
    AllowedMemberTypes = new List<string> { "User" },
    Description = RoleModel.RoleDescription,
    DisplayName = RoleModel.RoleName,
    Value = RoleModel.RoleName
 };
 application.AppRoles.Add(NewRole as AppRole);
 await application.UpdateAsync();

我还授予了所有应用程序权限,而不是从 Azure 门户到 Microsoft Graph 的委派权限API。但我收到此错误:

{"odata.error":{"code":"Authorization_RequestDenied","message":{"lang":"en","value":"Insufficient privileges to complete the operation."},"requestId":"e4187318-4b72-49fb-903d-42d419b65778","date":"2019-02-21T13:45:23"}}

注: 不过,我可以使用此访问令牌创建新用户并更新用户。

用于测试: 出于测试目的,我授予应用程序委派权限并使用客户端凭证流获取当前登录用户的访问令牌,如果登录用户具有足够的目录角色 he/she 可以在应用程序中创建角色,这工作正常。

问题: 那么,是否可以使用应用程序凭证流在应用程序中创建一个新角色?如果是这样,我是否遗漏了什么?

更新: 添加了 API Windows Azure Active Directory 的所有应用程序权限并授予管理员同意。

访问令牌: 从 ADzure AD

返回的访问令牌

Question: So, is it possible to create a new role in application using application credential flow? if so, am i missing something?

您的一般问题的答案是,您可以使用 Azure AD Graph API 和客户端凭据流向应用程序的角色添加新角色。

工作代码

下面给出了工作代码(它是一个快速而肮脏的控制台应用程序,只是为了确保我在确认之前对其进行测试)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.ActiveDirectory.GraphClient;
using Microsoft.IdentityModel.Clients.ActiveDirectory;

namespace AddAzureADApplicationRoles
{
    class Program
    {
        static void Main(string[] args)
        {
            ActiveDirectoryClient activeDirectoryClient = new ActiveDirectoryClient(new Uri("https://graph.windows.net/{myTenantId}"),
                async () => await GetTokenForApplication());

            //Fetch application Data from azure AD
            IApplication application = activeDirectoryClient.Applications.GetByObjectId("{MyAppObjectId}").ExecuteAsync().GetAwaiter().GetResult();

            AppRole NewRole = new AppRole
            {
                Id = Guid.NewGuid(),
                IsEnabled = true,
                AllowedMemberTypes = new List<string> {"User"},
                Description = "My Role Description..",
                DisplayName = "My Custom Role",
                Value = "MyCustomRole"
            };

            application.AppRoles.Add(NewRole as AppRole);
            application.UpdateAsync().GetAwaiter().GetResult();
        }

        public static async Task<string> GetTokenForApplication()
        {
            string TokenForApplication = "";

                AuthenticationContext authenticationContext = new AuthenticationContext(
                    "https://login.microsoftonline.com/{MyTenantId}",
                    false);

                // Configuration for OAuth client credentials 

                    ClientCredential clientCred = new ClientCredential("{AppId}",
                        "{AppSecret}"
                        );
                    AuthenticationResult authenticationResult =
                        await authenticationContext.AcquireTokenAsync("https://graph.windows.net", clientCred);
                    TokenForApplication = authenticationResult.AccessToken;                

            return TokenForApplication;
        }
    }
}

您的特定异常背后的可能问题

我认为您已在 Microsoft Graph API 上授予应用程序权限,而不是 Azure AD Graph API 所需的权限。

在为您的应用程序设置所需的权限时,在 Select 一个 API 对话框中,确保您 select "Windows Azure Active Directory" 而不是 "Microsoft Graph"。接下来我将提供更多详细信息的屏幕截图。

授予所需权限的步骤

请注意,我的应用不需要 "Microsoft Graph API" 的任何权限。它只有 "Windows Azure Active Directory".

的应用程序权限

因此,根据您的要求选择适当的应用程序权限,并确保您在最后执行 "Grant Permissions" 以提供管理员同意,因为此处所有应用程序权限都将需要管理员作为是。

附带说明一下,当您第一次创建应用程序注册时,它已经在 Windows Azure Active Directory 上拥有一个委派权限,因此您可能不需要显式 select Windows Azure Active Directory(除非您已为您的应用程序删除它),但只是 select 正确的应用程序权限并以管理员身份授予权限。