Beispiel #1
0
        /// <summary>
        /// Checks whether cookies still work, and tries to reconnect with cached username/password combo.
        /// </summary>
        /// <returns></returns>
        public async Task <(bool Success, string Message)> CheckAndReconnectSessionAsync()
        {
            // This will check whether the saved session still works
            var settings = await _db.GetSettingsAsync().ConfigureAwait(false);

            if (!string.IsNullOrEmpty(settings.CookieString))
            {
                var cookiestring = await StaticMethods.DecryptText(settings.CookieString);

                splitCookieString(cookiestring);
                // Do een request om te testen of cookies valid zijn
                var response = await getAsync(Constants.API_ENDPOINT + $"/year", cookiestring).ConfigureAwait(false);

                var forbidden = response.StatusCode == HttpStatusCode.Forbidden;
                if (!forbidden)
                {
                    // YES! sessie nog valid. we stellen gewoon lekker deze koekjes weer in.
                    return(true, "Re-auth via stored cookie success!");
                }
            }

            // als invalid koekjes, probeer nog 1x in te loggen met oude auth..
            var auth = await this.Authenticate(settings.Email, await StaticMethods.DecryptText(settings.Password)).ConfigureAwait(false);

            // Re-auth met login OK
            if (auth.Success)
            {
                return(true, "Re-auth via re-login success!");
            }

            // Re-auth niet mogelijk, return false en leeg koekjes.
            this._koekjes.Clear();
            return(false, "Re-auth failed!");
        }