我正在创建一个 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, Func
2 endFunction, Action1 endAction, Task
1 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权限,如下图所示:
获取访问 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/