public virtual async ValueTask <AccessToken> AuthenticateAsync(bool async, string[] scopes, CancellationToken cancellationToken) { IManagedIdentitySource identitySource = await GetManagedIdentitySourceAsync(async, cancellationToken).ConfigureAwait(false); // if msi is unavailable or we were unable to determine the type return CredentialUnavailable exception that no endpoint was found if (identitySource == default) { throw new CredentialUnavailableException(MsiUnavailableError); } using Request request = identitySource.CreateRequest(scopes); Response response = async ? await _pipeline.HttpPipeline.SendRequestAsync(request, cancellationToken).ConfigureAwait(false) : _pipeline.HttpPipeline.SendRequest(request, cancellationToken); if (response.Status == 200) { using JsonDocument json = async ? await JsonDocument.ParseAsync(response.ContentStream, default, cancellationToken).ConfigureAwait(false) : JsonDocument.Parse(response.ContentStream); (JsonElement accessToken, JsonElement expiresOnProp) = GetAccessTokenProperties(json.RootElement); return(identitySource.GetAccessTokenFromJson(accessToken, expiresOnProp)); } await identitySource.HandleFailedRequestAsync(response, _pipeline.Diagnostics, async).ConfigureAwait(false); throw async ? await _pipeline.Diagnostics.CreateRequestFailedExceptionAsync(response).ConfigureAwait(false) : _pipeline.Diagnostics.CreateRequestFailedException(response); }
private protected virtual async ValueTask <IManagedIdentitySource> GetManagedIdentitySourceAsync(bool async, CancellationToken cancellationToken) { using var asyncLock = await _identitySourceAsyncLock.GetLockOrValueAsync(async, cancellationToken).ConfigureAwait(false); if (asyncLock.HasValue) { return(asyncLock.Value); } IManagedIdentitySource identitySource = AppServiceV2017ManagedIdentitySource.TryCreate(_pipeline.HttpPipeline, ClientId) ?? CloudShellManagedIdentitySource.TryCreate(_pipeline.HttpPipeline, ClientId) ?? await ImdsManagedIdentitySource.TryCreateAsync(_pipeline.HttpPipeline, ClientId, async, cancellationToken).ConfigureAwait(false); asyncLock.SetValue(identitySource); return(identitySource); }