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

时间:2019-02-21 14:15:57

标签: azure azure-active-directory azure-ad-graph-api

我正在使用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

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

enter image description here

1 个答案:

答案 0 :(得分: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(除非您已将其删除了)应用程序),但只需选择正确的应用程序权限,然后以管理员身份执行“授予权限”即可。