【发布时间】:2016-09-30 17:19:45
【问题描述】:
我正在创建一个 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 requiresSynchronization)
--- 从之前抛出异常的位置结束堆栈跟踪 ---
在 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());
}
我启用了应用程序本身的所有许可并委托了许可,但仍然出现授权异常。我怎样才能在这里找到错误?我该如何解决这个问题?
最好的问候, 延斯
【问题讨论】:
标签: c# azure azure-active-directory azure-ad-graph-api