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)));
            }
        }
Exemple #2
0
        private async ValueTask <ExtendedAccessToken> GetTokenImplAsync(TokenRequestContext requestContext, CancellationToken cancellationToken)
        {
            using CredentialDiagnosticScope scope = _pipeline.StartGetTokenScope("Azure.Identity.ManagedIdentityCredential.GetToken", requestContext);

            try
            {
                MsiType msiType = await _client.GetMsiTypeAsync(cancellationToken).ConfigureAwait(false);

                // if msi is unavailable or we were unable to determine the type return a default access token
                if (msiType == MsiType.Unavailable || msiType == MsiType.Unknown)
                {
                    return(new ExtendedAccessToken(scope.Failed(new CredentialUnavailableException(MsiUnavailableError))));
                }

                AccessToken token = await _client.AuthenticateAsync(msiType, requestContext.Scopes, _clientId, cancellationToken).ConfigureAwait(false);

                return(new ExtendedAccessToken(scope.Succeeded(token)));
            }
            catch (OperationCanceledException e)
            {
                scope.Failed(e);

                throw;
            }
            catch (Exception e)
            {
                return(new ExtendedAccessToken(scope.Failed(e)));
            }
        }
Exemple #3
0
        private async ValueTask <AccessToken> GetTokenImplAsync(bool async, TokenRequestContext requestContext, CancellationToken cancellationToken)
        {
            using CredentialDiagnosticScope scope = _pipeline.StartGetTokenScope("ManagedIdentityCredential.GetToken", requestContext);

            try
            {
                AccessToken result = await _client.AuthenticateAsync(async, requestContext, cancellationToken).ConfigureAwait(false);

                return(scope.Succeeded(result));
            }
            catch (Exception e)
            {
                throw scope.FailWrapAndThrow(e);
            }
        }
Exemple #4
0
        private async ValueTask <AccessToken> GetTokenImplAsync(bool async, TokenRequestContext requestContext, CancellationToken cancellationToken)
        {
            using CredentialDiagnosticScope scope = _pipeline.StartGetTokenScope("ManagedIdentityCredential.GetToken", requestContext);

            try
            {
                AccessToken result = await _client.AuthenticateAsync(async, requestContext, cancellationToken).ConfigureAwait(false);

                if (_logAccountDetails)
                {
                    var accountDetails = TokenHelper.ParseAccountInfoFromToken(result.Token);
                    AzureIdentityEventSource.Singleton.AuthenticatedAccountDetails(accountDetails.ClientId ?? _clientId, accountDetails.TenantId, accountDetails.Upn, accountDetails.ObjectId);
                }
                return(scope.Succeeded(result));
            }
            catch (Exception e)
            {
                throw scope.FailWrapAndThrow(e, Troubleshooting);
            }
        }
Exemple #5
0
 /// <summary>
 /// Obtains an <see cref="AccessToken"/> from the Managed Identity service if available.
 /// </summary>
 /// <param name="scopes">The list of scopes for which the token will have access.</param>
 /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
 /// <returns>An <see cref="AccessToken"/> which can be used to authenticate service client calls, or a default <see cref="AccessToken"/> if no managed identity is available.</returns>
 public override async Task <AccessToken> GetTokenAsync(string[] scopes, CancellationToken cancellationToken = default)
 {
     return(await _client.AuthenticateAsync(scopes, _clientId, cancellationToken).ConfigureAwait(false));
 }
Exemple #6
0
 /// <summary>
 /// Obtains an <see cref="AccessToken"/> from the Managed Identity service if available.
 /// </summary>
 /// <param name="requestContext">The details of the authentication request.</param>
 /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
 /// <returns>An <see cref="AccessToken"/> which can be used to authenticate service client calls, or a default <see cref="AccessToken"/> if no managed identity is available.</returns>
 public override async ValueTask <AccessToken> GetTokenAsync(TokenRequestContext requestContext, CancellationToken cancellationToken = default)
 {
     return(await _client.AuthenticateAsync(requestContext.Scopes, _clientId, cancellationToken).ConfigureAwait(false));
 }