public void SaveCredentials(IAzureForMobileCredentials credentials)
        {
            if (credentials == null)
                return;

            Settings.AzureForMobileIdentityProvider = credentials.Provider;
            Settings.AzureForMobileIdentityUserId = credentials.User.UserId;
            Settings.AzureForMobileIdentityAuthToken = credentials.User.MobileServiceAuthenticationToken;
        }
        public bool TryLoadCredentials(out IAzureForMobileCredentials credentials)
        {
            credentials = !string.IsNullOrEmpty(Settings.AzureForMobileIdentityUserId)
                          && !string.IsNullOrEmpty(Settings.AzureForMobileIdentityAuthToken)
                          && Settings.AzureForMobileIdentityProvider != AzureForMobileAuthenticationProvider.None
                ? new AzureForMobileCredentials(Settings.AzureForMobileIdentityProvider, new MobileServiceUser(Settings.AzureForMobileIdentityUserId)
                    {
                        MobileServiceAuthenticationToken = Settings.AzureForMobileIdentityAuthToken
                    })
                : null;

            return credentials != null;
        }
        protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var response = await base.SendAsync(request, cancellationToken);

            if (response.StatusCode == HttpStatusCode.Unauthorized)
            {
                // Resolve IMvxAmsService if not yet defined
                if (AzureForMobileService == null)
                {
                    throw new InvalidOperationException("Make sure to configure AzureForMobile plugin properly before using it.");
                }

                // Cloning the request
                var clonedRequest = await CloneRequest(request);

                // Load saved user if possible
                if (_configuration.CredentialsCacheService != null
                    && _configuration.CredentialsCacheService.TryLoadCredentials(out _credentials)
                    && (AzureForMobileService.Identity.CurrentUser == null
                    || (AzureForMobileService.Identity.CurrentUser.UserId != _credentials.User.UserId
                    && AzureForMobileService.Identity.CurrentUser.MobileServiceAuthenticationToken != _credentials.User.MobileServiceAuthenticationToken)))
                {
                    AzureForMobileService.Identity.CurrentUser = _credentials.User;

                    clonedRequest.Headers.Remove("X-ZUMO-AUTH");
                    // Set the authentication header
                    clonedRequest.Headers.Add("X-ZUMO-AUTH", _credentials.User.MobileServiceAuthenticationToken);

                    // Resend the request
                    response = await base.SendAsync(clonedRequest, cancellationToken);
                }

                if (response.StatusCode == HttpStatusCode.Unauthorized
                    && _credentials != null
                    && _credentials.Provider != AzureForMobileAuthenticationProvider.None
                    && _credentials.Provider != AzureForMobileAuthenticationProvider.Custom)
                {
                    try
                    {
                        // Login user again
                        var user = await AzureForMobileService.Identity.LoginAsync(_credentials.Provider);

                        // Save the user if possible
                        if (_credentials == null) _credentials = new AzureForMobileCredentials(_credentials.Provider, user);
                        _configuration.CredentialsCacheService?.SaveCredentials(_credentials);

                        clonedRequest.Headers.Remove("X-ZUMO-AUTH");
                        // Set the authentication header
                        clonedRequest.Headers.Add("X-ZUMO-AUTH", user.MobileServiceAuthenticationToken);

                        // Resend the request
                        response = await base.SendAsync(clonedRequest, cancellationToken);
                    }
                    catch (InvalidOperationException)
                    {
                        // user cancelled auth, so lets return the original response
                        return response;
                    }
                }

                if (response.StatusCode == HttpStatusCode.Unauthorized)
                {
                    _onLoggedOut?.Invoke();
                }
            }

            return response;
        }