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));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Sequentially calls <see cref="TokenCredential.GetToken"/> on all the specified sources, returning the first successfully obtained <see cref="AccessToken"/>. This method is called by Azure SDK clients. It isn't intended for use in application code.
        /// </summary>
        /// <param name="requestContext">The details of the authentication request.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
        /// <returns>The first <see cref="AccessToken"/> returned by the specified sources. Any credential which raises a <see cref="CredentialUnavailableException"/> will be skipped.</returns>
        public override async ValueTask <AccessToken> GetTokenAsync(TokenRequestContext requestContext, CancellationToken cancellationToken = default)
        {
            List <Exception> exceptions = new List <Exception>();

            for (int i = 0; i < _sources.Length; i++)
            {
                try
                {
                    return(await _sources[i].GetTokenAsync(requestContext, cancellationToken).ConfigureAwait(false));
                }
                catch (CredentialUnavailableException e)
                {
                    exceptions.Add(e);
                }
                catch (Exception e) when(!(e is OperationCanceledException))
                {
                    exceptions.Add(e);

                    throw AuthenticationFailedException.CreateAggregateException(AggregateCredentialFailedErrorMessage + e.Message, new ReadOnlyMemory <object>(_sources, 0, i + 1), exceptions);
                }
            }

            throw AuthenticationFailedException.CreateAggregateException(AggregateAllUnavailableErrorMessage, _sources, exceptions);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Sequentially calls <see cref="TokenCredential.GetToken"/> on all the specified sources, returning the first successfully obtained <see cref="AccessToken"/>. This method is called by Azure SDK clients. It isn't intended for use in application code.
        /// </summary>
        /// <param name="requestContext">The details of the authentication request.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
        /// <returns>The first <see cref="AccessToken"/> returned by the specified sources. Any credential which raises a <see cref="CredentialUnavailableException"/> will be skipped.</returns>
        public override AccessToken GetToken(TokenRequestContext requestContext, CancellationToken cancellationToken = default)
        {
            List <Exception> exceptions = new List <Exception>();

            for (int i = 0; i < _sources.Length; i++)
            {
                try
                {
                    return(_sources[i].GetToken(requestContext, cancellationToken));
                }
                catch (CredentialUnavailableException e)
                {
                    exceptions.Add(e);
                }
                catch (Exception e) when(!(e is OperationCanceledException))
                {
                    exceptions.Add(e);

                    throw AuthenticationFailedException.CreateAggregateException(AggregateCredentialFailedErrorMessage + e.Message, exceptions);
                }
            }

            throw AuthenticationFailedException.CreateAggregateException(AggregateAllUnavailableErrorMessage, exceptions);
        }