Exemple #1
0
        private async Task <AuthenticationResult> GetNewAccessTokenAsync(CancellationToken cancellationToken, AuthenticationProviderOption msalAuthProviderOption)
        {
            AuthenticationResult authenticationResult = null;
            int    retryCount          = 0;
            string extraQueryParameter = null;

            do
            {
                try
                {
                    authenticationResult = await ClientApplication.AcquireTokenWithDeviceCode(msalAuthProviderOption.Scopes, DeviceCodeResultCallback)
                                           .WithExtraQueryParameters(extraQueryParameter)
                                           .ExecuteAsync(cancellationToken);

                    break;
                }
                catch (MsalServiceException serviceException)
                {
                    if (serviceException.ErrorCode == ErrorConstants.Codes.TemporarilyUnavailable)
                    {
                        TimeSpan delay = this.GetRetryAfter(serviceException);
                        retryCount++;
                        // pause execution
                        await Task.Delay(delay);
                    }
                    else if (serviceException.Claims != null)
                    {
                        extraQueryParameter = $"claims={serviceException.Claims}";
                        retryCount++;
                    }
                    else
                    {
                        throw new AuthenticationException(
                                  new Error
                        {
                            Code    = ErrorConstants.Codes.GeneralException,
                            Message = ErrorConstants.Message.UnexpectedMsalException
                        },
                                  serviceException);
                    }
                }
                catch (Exception exception)
                {
                    throw new AuthenticationException(
                              new Error
                    {
                        Code    = ErrorConstants.Codes.GeneralException,
                        Message = ErrorConstants.Message.UnexpectedException
                    },
                              exception);
                }
            } while (retryCount < msalAuthProviderOption.MaxRetry);

            return(authenticationResult);
        }