Azure AD Graph - 使用Application Credential Flow创建AppRole

问题描述 投票:2回答:1

我正在使用Azure AD Graph API在azure应用程序中创建一个新角色。我正在做的是使用以下代码从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”:“没有足够的权限来完成操作。”},“requestId”:“e4187318 -4b72-49fb-903D-42d419b65778" , “日期”: “2019-02-21T13:45:23”}}

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

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

问题:那么,是否可以使用应用程序凭据流在应用程序中创建新角色?如果是的话,我错过了什么吗?

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

enter image description here

访问令牌:从Azure AD返回的访问令牌

enter image description here

azure azure-active-directory azure-ad-graph-api
1个回答
1
投票

问题:那么,是否可以使用应用程序凭据流在应用程序中创建新角色?如果是的话,我错过了什么吗?

回答一般问题是的,您可以使用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所需的权限。

在为应用程序设置所需权限时,在“选择API”对话框中,确保选择“Windows Azure Active Directory”而不是“Microsoft Graph”。我接下来将详细介绍截图。

提供所需权限的步骤

请注意,我的应用程序不需要“Microsoft Graph API”的任何权限。它只具有“Windows Azure Active Directory”的应用程序权限。

因此,请为您的要求选择适当的应用程序权限,并确保在最后执行“授予权限”以提供管理员同意,因为此处的所有应用程序权限都提到需要管理员为是。

enter image description here

另外,当您首次创建应用程序注册时,它已在Windows Azure Active Directory上拥有一个委派权限,因此您可能不需要再次显式选择Windows Azure Active Directory(除非您已为应用程序删除它),但只需选择正确的应用程序权限并以管理员身份授予权限。

© www.soinside.com 2019 - 2024. All rights reserved.