/// <summary>
        /// Retrieves the authentication token.
        /// </summary>
        /// <returns>The authentication token.</returns>
        public async Task <AccountSession> AuthenticateAsync()
        {
            if (this.CurrentAccountSession != null && !this.CurrentAccountSession.IsExpiring())
            {
                return(this.CurrentAccountSession);
            }
            else if (this.CurrentAccountSession != null && !string.IsNullOrEmpty(this.CurrentAccountSession.RefreshToken))
            {
                // We will have tried to authenticate silently using ADAL before hitting this point. It's safe to try
                // re-auth using refresh token if we don't have a valid token by now.
                if (this.adalRedeemRefreshTokenHelper == null)
                {
                    this.adalRedeemRefreshTokenHelper = new AdalRedeemRefreshTokenHelper(
                        this.ServiceInfo,
                        this.authenticationContextWrapper);
                }

                var refreshResult = await this.adalRedeemRefreshTokenHelper.RedeemRefreshToken(this.CurrentAccountSession.RefreshToken);

                return(this.ProcessAuthenticationResult(refreshResult));
            }

            if (allowDiscoveryService && string.IsNullOrEmpty(this.ServiceInfo.ServiceResource) || string.IsNullOrEmpty(this.ServiceInfo.BaseUrl))
            {
                var discoveryServiceToken = await this.GetAuthenticationTokenForResourceAsync(this.serviceInfo.DiscoveryServiceResource);

                await this.RetrieveMyFilesServiceResourceAsync(discoveryServiceToken);
            }

            var authenticationResult = await this.AuthenticateResourceAsync(this.ServiceInfo.ServiceResource);

            return(this.ProcessAuthenticationResult(authenticationResult));
        }
        /// <summary>
        /// Retrieves the authentication token.
        /// </summary>
        /// <returns>The authentication token.</returns>
        public async Task<AccountSession> AuthenticateAsync()
        {
            if (this.CurrentAccountSession != null && !this.CurrentAccountSession.IsExpiring())
            {
                return this.CurrentAccountSession;
            }
            else if (this.CurrentAccountSession != null && !string.IsNullOrEmpty(this.CurrentAccountSession.RefreshToken))
            {
                // We will have tried to authenticate silently using ADAL before hitting this point. It's safe to try
                // re-auth using refresh token if we don't have a valid token by now.
                if (this.adalRedeemRefreshTokenHelper == null)
                {
                    this.adalRedeemRefreshTokenHelper = new AdalRedeemRefreshTokenHelper(
                        this.ServiceInfo,
                        this.authenticationContextWrapper);
                }

                var refreshResult = await this.adalRedeemRefreshTokenHelper.RedeemRefreshToken(this.CurrentAccountSession.RefreshToken);

                return this.ProcessAuthenticationResult(refreshResult);
            }
            
            if (allowDiscoveryService && string.IsNullOrEmpty(this.ServiceInfo.ServiceResource) || string.IsNullOrEmpty(this.ServiceInfo.BaseUrl))
            {
                var discoveryServiceToken = await this.GetAuthenticationTokenForResourceAsync(this.serviceInfo.DiscoveryServiceResource);
                await this.RetrieveMyFilesServiceResourceAsync(discoveryServiceToken);
            }

            var authenticationResult = await this.AuthenticateResourceAsync(this.ServiceInfo.ServiceResource);

            return this.ProcessAuthenticationResult(authenticationResult);
        }