/// <summary>
        /// Process errors.
        /// </summary>
        /// <param name="e">The current error.</param>
        /// <param name="index">The current certificate index.</param>
        /// <param name="errors">The current errors.</param>
        /// <param name="result">The result when returning early.</param>
        /// <returns>'true' to return the result early.</returns>
        private static bool TryHandleError(Exception e, int index, ICollection <Exception> errors, out AcquireTokenResult result)
        {
            // record the error of the failed certificate
            ADXTrace.Instance.TraceWarning(TraceCategory.Application, string.Format("Get token failed: index: {0}: {1}", index, e));

            var ase = e as AdalServiceException;

            if (ase != null)
            {
                if (ase.ErrorCode == "AADSTS50001")
                {
                    // return fatal error immediately
                    var aseError = new PortalAdalServiceException("CRM is disabled.", ase);
                    result = new AcquireTokenResult {
                        Token = null, Error = aseError
                    };
                    return(true);
                }

                errors.Add(new PortalAdalServiceException("Error Connecting to CRM.", ase));
            }
            else
            {
                errors.Add(e);
            }

            result = default(AcquireTokenResult);
            return(false);
        }
        /// <summary>
        /// Updates the cached token.
        /// </summary>
        /// <param name="result">The new token.</param>
        private void ApplyAuthenticationResult(AcquireTokenResult result)
        {
            this.tokenCache = result.Token;

            if (result.Error != null)
            {
                throw result.Error;
            }

            if (this.tokenCache != null)
            {
                var duration = this.tokenCache.ExpiresOn - DateTimeOffset.UtcNow;
                ADXTrace.Instance.TraceInfo(TraceCategory.Application, string.Format("New token expires on: {0}: after: {1}", this.tokenCache.ExpiresOn, duration));
            }
        }