使用服务主体创建 Azure AD 应用程序时权限不足,无法完成操作

Insufficient privileges to complete the operation when using service principal to create Azure AD Application

遵循此处概述的代码:

https://github.com/Azure-Samples/active-directory-dotnet-graphapi-console/blob/master/GraphConsoleAppV3/Program.cs#L810

但是在

上尝试创建应用程序时,使用服务委托人进行身份验证会出现权限不足错误
client.Applications.AddApplicationAsync(appObject).Wait();

代币交换如下:

var context = new AuthenticationContext($"https://login.microsoftonline.com/{tenantId}");
var token = context.AcquireToken("https://graph.windows.net", new ClientCredential(clientId, secret));    
var client = new ActiveDirectoryClient(new Uri($"https://graph.windows.net/{tenantId}"),()=>Task.FromResult(token.AccessToken));

它要求应用程序已被授予管理员角色,目前似乎只能使用 Azure AD 的 powershell 工具添加:https://msdn.microsoft.com/library/azure/jj151815.aspx#bkmk_installmodule

并使用 connect-msolservice 并在出现提示时使用您的 Azure Ad 管理员用户登录。

  1. connect-msolservice
  2. Get-MsolServicePrincipal –AppPrincipalId {appId}
  3. Add-MsolRoleMember -RoleMemberType ServicePrincipal -RoleName 'Company Administrator' -RoleMemberObjectId {objectID}

其中 objectID 是 2 命令的输出