public virtual AlibabaCloudCredentials GetCredentials()
        {
            try
            {
                if (credentials == null)
                {
                    credentials = fetcher.Fetch(maxRetryTimes);
                }

                if (credentials.IsExpired())
                {
                    throw new ClientException("SDK.SessionTokenExpired", "Current session token has expired.");
                }

                if (!credentials.WillSoonExpire() || !credentials.ShouldRefresh())
                {
                    return(credentials);
                }

                credentials = fetcher.Fetch();
                return(credentials);
            }
            catch (ClientException ex)
            {
                if (ex.ErrorCode.Equals("SDK.SessionTokenExpired") && ex.ErrorMessage.Equals("Current session token has expired."))
                {
                    SerilogHelper.LogException(ex, ex.ErrorCode, ex.ErrorMessage);
                    throw new ClientException(ex.ErrorCode, ex.ErrorMessage);
                }

                // Use the current expiring session token and wait for next round
                credentials.SetLastFailedRefreshTime();
            }
            return(credentials);
        }
        AlibabaCloudCredentials AlibabaCloudCredentialsProvider.GetCredentials()
        {
            if (credentials == null)
            {
                ecsMetadataServiceFetchCount += 1;
                credentials = fetcher.Fetch(maxRetryTimes);
            }

            if (credentials.IsExpired())
            {
                throw new ClientException("SDK.SessionTokenExpired", "Current session token has expired.");
            }
            else if (credentials.WillSoonExpire() && credentials.ShouldRefresh())
            {
                try
                {
                    ecsMetadataServiceFetchCount += 1;
                    credentials = fetcher.Fetch();
                }
                catch (ClientException)
                {
                    // Use the current expiring session token and wait for next round
                    credentials.SetLastFailedRefreshTime();
                }
            }
            return(credentials);
        }
Beispiel #3
0
        public async Task <AlibabaCloudCredentials> GetCredentialsAsync(CancellationToken cancellationToken)
        {
            try
            {
                if (credentials == null)
                {
                    credentials = await fetcher.FetchAsync(maxRetryTimes, cancellationToken).ConfigureAwait(false);
                }

                if (credentials.IsExpired())
                {
                    throw new ClientException("SDK.SessionTokenExpired", "Current session token has expired.");
                }

                if (!credentials.WillSoonExpire() || !credentials.ShouldRefresh())
                {
                    return(credentials);
                }

                credentials = await fetcher.FetchAsync(cancellationToken).ConfigureAwait(false);

                return(credentials);
            }
            catch (ClientException ex)
            {
                if (ex.ErrorCode.Equals("SDK.SessionTokenExpired") &&
                    ex.ErrorMessage.Equals("Current session token has expired."))
                {
                    CommonLog.LogException(ex, ex.ErrorCode, ex.ErrorMessage);
                    throw new ClientException(ex.ErrorCode, ex.ErrorMessage);
                }

                // Use the current expiring session token and wait for next round
                credentials.SetLastFailedRefreshTime();
            }

            return(credentials);
        }