Ejemplo n.º 1
0
        private async Task <MsalTokenResponse> SendHttpAndClearTelemetryAsync(string tokenEndpoint, Core.ICoreLogger logger)
        {
            UriBuilder builder = new UriBuilder(tokenEndpoint);

            builder.AppendQueryParameters(_requestParams.ExtraQueryParameters);
            Uri tokenEndpointWithQueryParams = builder.Uri;

            try
            {
                logger.Verbose("[Token Client] Fetching MsalTokenResponse .... ");
                MsalTokenResponse msalTokenResponse =
                    await _oAuth2Client
                    .GetTokenAsync(tokenEndpointWithQueryParams,
                                   _requestParams.RequestContext, true, _requestParams.OnBeforeTokenRequestHandler)
                    .ConfigureAwait(false);

                // Clear failed telemetry data as we've just sent it
                _serviceBundle.HttpTelemetryManager.ResetPreviousUnsentData();

                return(msalTokenResponse);
            }
            catch (MsalServiceException ex)
            {
                if (!ex.IsAadUnavailable())
                {
                    // Clear failed telemetry data as we've just sent it ...
                    // even if we received an error from the server,
                    // telemetry would have been recorded
                    _serviceBundle.HttpTelemetryManager.ResetPreviousUnsentData();
                }

                if (ex.StatusCode == (int)HttpStatusCode.Unauthorized)
                {
                    string responseHeader = string.Empty;
                    var    isChallenge    = _serviceBundle.DeviceAuthManager.TryCreateDeviceAuthChallengeResponse(
                        ex.Headers,
                        new Uri(tokenEndpoint), // do not add query params to PKeyAuth https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/issues/2359
                        out responseHeader);
                    if (isChallenge)
                    {
                        //Injecting PKeyAuth response here and replaying request to attempt device auth
                        _oAuth2Client.AddHeader("Authorization", responseHeader);

                        return(await _oAuth2Client.GetTokenAsync(
                                   tokenEndpointWithQueryParams,
                                   _requestParams.RequestContext,
                                   false, _requestParams.OnBeforeTokenRequestHandler).ConfigureAwait(false));
                    }
                }

                throw;
            }
            finally
            {
                _requestInProgress = false;
            }
        }
Ejemplo n.º 2
0
        private async Task <MsalTokenResponse> SendHttpAndClearTelemetryAsync(string tokenEndpoint)
        {
            UriBuilder builder = new UriBuilder(tokenEndpoint);

            try
            {
                builder.AppendQueryParameters(_requestParams.ExtraQueryParameters);

                MsalTokenResponse msalTokenResponse =
                    await _oAuth2Client
                    .GetTokenAsync(builder.Uri,
                                   _requestParams.RequestContext)
                    .ConfigureAwait(false);

                // Clear failed telemetry data as we've just sent it
                _serviceBundle.HttpTelemetryManager.ResetPreviousUnsentData();

                return(msalTokenResponse);
            }
            catch (MsalServiceException ex)
            {
                if (!ex.IsAadUnavailable())
                {
                    // Clear failed telemetry data as we've just sent it ...
                    // even if we received an error from the server,
                    // telemetry would have been recorded
                    _serviceBundle.HttpTelemetryManager.ResetPreviousUnsentData();
                }

                if (ex.StatusCode == (int)HttpStatusCode.Unauthorized)
                {
                    string responseHeader = string.Empty;
                    var    isChallenge    = _serviceBundle.DeviceAuthManager.TryCreateDeviceAuthChallengeResponseAsync(ex.Headers, builder.Uri, out responseHeader);
                    if (isChallenge)
                    {
                        //Injecting PKeyAuth response here and replaying request to attempt device auth
                        _oAuth2Client.AddHeader("Authorization", responseHeader);

                        return(await _oAuth2Client.GetTokenAsync(builder.Uri, _requestParams.RequestContext, false).ConfigureAwait(false));
                    }
                }

                throw;
            }
            finally
            {
                _requestInProgress = false;
            }
        }
        private async Task <MsalTokenResponse> SendHttpAndClearTelemetryAsync(string tokenEndpoint)
        {
            UriBuilder builder = new UriBuilder(tokenEndpoint);

            builder.AppendQueryParameters(_requestParams.ExtraQueryParameters);

            try
            {
                MsalTokenResponse msalTokenResponse =
                    await _oAuth2Client
                    .GetTokenAsync(builder.Uri,
                                   _requestParams.RequestContext)
                    .ConfigureAwait(false);

                // Clear failed telemetry data as we've just sent it
                _serviceBundle.HttpTelemetryManager.ResetPreviousUnsentData();

                return(msalTokenResponse);
            }
            catch (MsalServiceException ex)
            {
                if (!ex.IsAadUnavailable())
                {
                    // Clear failed telemetry data as we've just sent it ...
                    // even if we received an error from the server,
                    // telemetry would have been recorded
                    _serviceBundle.HttpTelemetryManager.ResetPreviousUnsentData();
                }
                throw;
            }
            finally
            {
                _requestInProgress = false;
            }
        }
        private async Task <MsalTokenResponse> SendHttpMessageAsync(string tokenEndpoint)
        {
            UriBuilder builder = new UriBuilder(tokenEndpoint);

            builder.AppendQueryParameters(_requestParams.ExtraQueryParameters);
            MsalTokenResponse msalTokenResponse =
                await _oAuth2Client
                .GetTokenAsync(builder.Uri,
                               _requestParams.RequestContext)
                .ConfigureAwait(false);

            if (string.IsNullOrEmpty(msalTokenResponse.Scope))
            {
                msalTokenResponse.Scope = _requestParams.Scope.AsSingleString();
                _requestParams.RequestContext.Logger.Info("ScopeSet was missing from the token response, so using developer provided scopes in the result. ");
            }

            return(msalTokenResponse);
        }