Exemple #1
0
        private async Task <AuthenticationResult> GetNewAccessTokenAsync(IAccount account, AuthenticationProviderOption msalAuthProviderOption)
        {
            AuthenticationResult authenticationResult = null;

            int    retryCount          = 0;
            string extraQueryParameter = null;

            do
            {
                try
                {
                    var builder = ClientApplication.AcquireTokenInteractive(msalAuthProviderOption.Scopes)
                                  .WithAccount(account)
                                  .WithPrompt(Prompt)
                                  .WithExtraQueryParameters(extraQueryParameter)
                                  .WithExtraScopesToConsent(null)
                                  .WithAuthority(ClientApplication.Authority);
#if NET45
                    if (ParentWindow != null)
                    {
                        builder = builder.WithParentActivityOrWindow(ParentWindow);
                    }
                    else
                    {
                        builder = builder.WithParentActivityOrWindow(ParentPointer);
                    }
#elif NETSTANDARD1_3
                    builder = builder.WithParentActivityOrWindow(Parent);
#endif
                    authenticationResult = await builder.ExecuteAsync();

                    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);
        }