c# - 创建 AD 用户抛出 Authorization_RequestDenied : "Insufficient privileges to complete the operation." exception

标签 c# azure azure-active-directory azure-ad-graph-api

我正在创建一个 C# 控制台应用程序,该应用程序正在为特定租户读取和创建 Azure AD 帐户。 我添加了一个 Azure AD 应用程序并记下 AppId、AppKey( secret )、TenantId、TenantName。

为此,我们使用以下 NuGet 包:

从域读取 Azure AD 帐户工作正常,但创建新的 Azure AD 帐户会引发异常: activeDirectoryClient.Users.AddUserAsync(userToBeAdded).Wait();

“权限不足,无法完成操作。” "{\"odata.error\":{\"code\":\"Authorization_RequestDenied\",\"message\":{\"lang\":\"en\",\"value\":\"权限不足,无法完成操作。\"}}}" 在 System.Data.Services.Client.SaveResult.HandleResponse() 在 System.Data.Services.Client.BaseSaveResult.EndRequest() 在 System.Data.Services.Client.DataServiceContext.EndSaveChanges(IAsyncResult asyncResult) 在 System.Threading.Tasks.TaskFactory1.FromAsyncCoreLogic(IAsyncResult iar, Func2 endFunction, Action1 endAction, Task1 Promise, Boolean requireSynchronization) --- 从先前抛出异常的位置开始的堆栈跟踪结束 --- 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) 在 Microsoft.Azure.ActiveDirectory.GraphClient.Extensions.DataServiceContextWrapper.d__74.MoveNext()

由于控制台应用程序没有 UI,因此使用 AppId 和 Secret 执行身份验证。

public async Task<string> AcquireTokenAsyncForApplication()
{
AuthenticationContext authenticationContext = new AuthenticationContext("https://login.microsoftonline.com/" + tenantName, false);
// Config for OAuth client credentials 
ClientCredential clientCred = new ClientCredential("<appid>", "<appsecret>");
var authenticationResult = authenticationContext.AcquireTokenAsync("https://graph.windows.net", clientCred);
authenticationResult.Wait();            

return authenticationResult.Result.AccessToken;
}

public void Authenticate()
{
Uri servicePointUri = new Uri("https://graph.windows.net");
Uri serviceRoot = new Uri(servicePointUri, tenantId);
activeDirectoryClient = new ActiveDirectoryClient(serviceRoot,
async () => await AcquireTokenAsyncForApplication());
}

我启用了应用程序本身的所有权限和委托(delegate)权限,但授权异常仍然出现。我怎样才能找到这里的错误?我该如何解决这个问题?

最诚挚的问候, 延斯

最佳答案

要使用客户端凭据通过 Azure Graph REST 创建用户,我们可以为应用程序权限配置Directory.ReadWrite.All权限,如下图所示: enter image description here

获取访问 token 后,您可以从 here 解析 token 。并确保在创建用户之前 Directory.ReadWrite.All 位于 token 中。

关于c# - 创建 AD 用户抛出 Authorization_RequestDenied : "Insufficient privileges to complete the operation." exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39765719/

相关文章:

mysql - 如何将架构加载到 Node-MySQL ClearDB Azure 中?

azure - PowerShell 中的 New-AzADApplication : Key credential end date is invalid. 错误

asp.net-web-api - Azure AD OAuth 客户端凭据流访问控制

c# - 如何将用户分配到 Azure 资源组

c# - SSDT 如何从 DACPAC 文件创建新数据库?

c# - 如何在不知道 C# 中原始编码的情况下将字符串的编码转换为 UTF-8?

c# - 如何通过 Visual Studio 创建和运行 NUnit/WebDriver 测试套件?

c# - Web 服务无法使用 GAC 中的类型创建类型错误

azure - 从逻辑应用程序如何调用某些本地服务器上私有(private)托管的基于 SOAP 的 Web 服务

wcf - 我可以在 Azure 网站上打开端口吗?