private async ValueTask <ExtendedAccessToken> GetTokenImplAsync(TokenRequestContext requestContext, CancellationToken cancellationToken) { using CredentialDiagnosticScope scope = _pipeline.StartGetTokenScope("ManagedIdentityCredential.GetToken", requestContext); try { ExtendedAccessToken result = await _client.AuthenticateAsync(requestContext.Scopes, cancellationToken).ConfigureAwait(false); if (result.Exception != null) { scope.Failed(result.Exception); } else { scope.Succeeded(result.AccessToken); } return(result); } catch (OperationCanceledException e) { scope.Failed(e); throw; } catch (Exception e) { return(new ExtendedAccessToken(scope.Failed(e))); } }
private async Task <AccessToken> GetTokenAsync(bool isAsync, TokenRequestContext requestContext, CancellationToken cancellationToken) { using CredentialDiagnosticScope scope = _pipeline.StartGetTokenScope("Azure.Identity.DefaultAcureCredential.GetToken", requestContext); List <Exception> exceptions = new List <Exception>(); int i; for (i = 0; i < _sources.Length && _sources[i] != null; i++) { ExtendedAccessToken exToken = isAsync ? await _sources[i].GetTokenAsync(requestContext, cancellationToken).ConfigureAwait(false) : _sources[i].GetToken(requestContext, cancellationToken); if (exToken.Exception is null) { return(scope.Succeeded(exToken.AccessToken)); } if (exToken.Exception is CredentialUnavailableException) { exceptions.Add(exToken.Exception); } else { exceptions.Add(exToken.Exception); throw scope.Failed(AuthenticationFailedException.CreateAggregateException($"{UnhandledExceptionMessage} {_sources[i].GetType().Name} failed with unhandled exception {exToken.Exception.Message}.", new ReadOnlyMemory <object>(_sources, 0, i + 1), exceptions)); } } throw scope.Failed(AuthenticationFailedException.CreateAggregateException(DefaultExceptionMessage, new ReadOnlyMemory <object>(_sources, 0, i), exceptions)); }
private async Task <AccessToken> GetTokenAsync(bool isAsync, TokenRequestContext requestContext, CancellationToken cancellationToken) { using CredentialDiagnosticScope scope = _pipeline.StartGetTokenScope("DefaultAzureCredential.GetToken", requestContext); List <Exception> exceptions = new List <Exception>(); int i; for (i = 0; i < _sources.Length && _sources[i] != null; i++) { ExtendedAccessToken exToken = isAsync ? await _sources[i].GetTokenAsync(requestContext, cancellationToken).ConfigureAwait(false) : _sources[i].GetToken(requestContext, cancellationToken); if (exToken.Exception is null) { return(scope.Succeeded(exToken.AccessToken)); } if (exToken.Exception is CredentialUnavailableException) { exceptions.Add(exToken.Exception); } else { throw scope.Failed(new AuthenticationFailedException(UnhandledExceptionMessage, exToken.Exception)); } } StringBuilder errorMsg = new StringBuilder(DefaultExceptionMessage); foreach (Exception ex in exceptions) { errorMsg.Append(Environment.NewLine).Append(ex.Message); } throw scope.Failed(new CredentialUnavailableException(errorMsg.ToString())); }