.NETCORE 2.0 AcquireTokenAsync引发AdalServiceException:TaskCanceledException

时间:2018-08-09 06:40:00

标签: .net-core adal

我正在使用以下代码在.DOTNET core 2.0上使用ADAL库的3.16版本获取AAD令牌,并在1分钟后始终获取TaskCancelledException。即使在重试时,这种情况也会发生。

var authenticationContext = new AuthenticationContext(auth);
if (authenticationContext == null)
throw (new ArgumentNullException("AuthenticationContext")); authenticationContext.TokenCache.Clear();
ClientAssertionCertificate clientAssertion = new ClientAssertionCertificate(clientId, certificate);

AuthenticationResult authenticationResult = authenticationContext.AcquireTokenAsync(audience, clientAssertion).Result;
  

System.AggregateException:发生一个或多个错误。 (未知错误:未知错误)---> Microsoft.IdentityModel.Clients.ActiveDirectory.AdalServiceException:未知错误:未知错误---> System.Threading.Tasks.TaskCanceledException:任务被取消。   在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)   在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)   在System.Net.Http.HttpClient.d__58.MoveNext()处的System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult()-从上次引发异常的位置开始的堆栈跟踪-在System.Runtime。 Microsoft.IdentityModel.Clients.ActiveDirectory.Http(30.30。 )-内部异常堆栈跟踪的结尾--在Microsoft.IdentityModel.Clients.ActiveDirectory.AdalHttpClient.d__211.MoveNext()   ---从之前引发异常的位置开始的堆栈结束跟踪---   在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()   在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)   在Microsoft.IdentityModel.Clients.ActiveDirectory.AdalHttpClient.d__211.MoveNext()处-从上一个引发异常的位置开始的堆栈跟踪-在System.Runtime处的System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()处。 Microsoft.IdentityModel.Clients.ActiveDirectory.AdalHttpClient.d__201.MoveNext()上的CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)   ---从之前引发异常的位置开始的堆栈结束跟踪---   在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()   在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)   在Microsoft.IdentityModel.Clients.ActiveDirectory.AcquireTokenHandlerBase.d__67.MoveNext()   ---从之前引发异常的位置开始的堆栈结束跟踪---   在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()   在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)   在Microsoft.IdentityModel.Clients.ActiveDirectory.AcquireTokenHandlerBase.d__64.MoveNext()   ---从之前引发异常的位置开始的堆栈结束跟踪---   在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()   在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)   在Microsoft.IdentityModel.Clients.ActiveDirectory.AcquireTokenHandlerBase.d__55.MoveNext()   ---从之前引发异常的位置开始的堆栈结束跟踪---   在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()   在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)   在Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext.d__51.MoveNext()   ---从之前引发异常的位置开始的堆栈结束跟踪---   在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()   在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)   在Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext.d__27.MoveNext()   ---内部异常堆栈跟踪的结尾---   在System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification)在C:\ Users \ risraj \ source \ repos \ TestAADCallsForLinux \ TestAADCallsForLinux \ Program.cs:第87行的TestAADCallsForLinux.Program.FetchAADToken(StringregisteredObjectInfo) (内部异常#0)Microsoft.IdentityModel.Clients.ActiveDirectory.AdalServiceException:unknown_error:未知错误---> System.Threading.Tasks.TaskCanceledException:任务被取消。在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)在System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult()   在System.Net.Http.HttpClient.d__58.MoveNext()   ---从之前引发异常的位置开始的堆栈结束跟踪---   在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()   在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)   在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)   在Microsoft.IdentityModel.Clients.ActiveDirectory.HttpClientWrapper.d__30.MoveNext()   ---内部异常堆栈跟踪的结尾---   在Microsoft.IdentityModel.Clients.ActiveDirectory.AdalHttpClient.d__211.MoveNext()处-从上一个引发异常的位置开始的堆栈跟踪-在System.Runtime处的System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()处。 Microsoft.IdentityModel.Clients.ActiveDirectory.AdalHttpClient.d__211.MoveNext()上的CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)   ---从之前引发异常的位置开始的堆栈结束跟踪---   在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()   在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)   在Microsoft.IdentityModel.Clients.ActiveDirectory.AdalHttpClient.d__20`1.MoveNext()   ---从之前引发异常的位置开始的堆栈结束跟踪---   在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()   在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)   在Microsoft.IdentityModel.Clients.ActiveDirectory.AcquireTokenHandlerBase.d__67.MoveNext()   ---从之前引发异常的位置开始的堆栈结束跟踪---   在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()   在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)   在Microsoft.IdentityModel.Clients.ActiveDirectory.AcquireTokenHandlerBase.d__64.MoveNext()   ---从之前引发异常的位置开始的堆栈结束跟踪---   在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()   在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)   在Microsoft.IdentityModel.Clients.ActiveDirectory.AcquireTokenHandlerBase.d__55.MoveNext()   ---从之前引发异常的位置开始的堆栈结束跟踪---   在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()   在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)   在Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext.d__51.MoveNext()   ---从之前引发异常的位置开始的堆栈结束跟踪---   在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()   在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)   在Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext.d__27.MoveNext()   错误代码:unknown_error   状态码:0 <---

1 个答案:

答案 0 :(得分:0)

可能是线程问题...您需要使用async / await模式,否则会遇到多线程问题。有关更多信息,请参见wiki