public bool AuthCheckTokenExpiration() { string expStr = AuthenticationHelper.GetExpirationUTC(); if (expStr != null) { DateTime expDate; if (DateTime.TryParse(expStr, out expDate)) { if (expDate > DateTime.UtcNow.AddMinutes(MINUTES_REMAINING_FOR_TRIGGER_REFRESH_TOKEN)) { //token is fine now and valid for more than the next ___ minutes, it doesn't need a refresh return(true); } else { //token is expired or will expire in the next ____ minutes, needs a refresh return(false); } } else { //can't parse expiration date, just clear and refresh everything and log it Analytics.TrackEvent($"Can't parse this token expiration date: {expStr}"); AuthenticationHelper.ClearSecureStorageAuthValues(); return(false); } } else { return(false); } }
public async Task <bool> AuthGetToken(string user, string pass) { try { var content = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("grant_type", "password"), new KeyValuePair <string, string>("username", user), new KeyValuePair <string, string>("password", pass) }); var client = GetHttpClient(Consts.UNAUTHORIZED); var result = await client.PostAsync("token", content); if (result.IsSuccessStatusCode) { var resultContent = JsonConvert.DeserializeObject <AuthenticationResult>(await result.Content.ReadAsStringAsync()); AuthenticationHelper.SetTokens(resultContent.access_token, resultContent.refresh_token, DateTime.UtcNow.AddSeconds((double)resultContent.expires_in).ToString()); Preferences.Set(Consts.CURRENT_ASP_USER_ID, resultContent.userId); Preferences.Set(Consts.CURRENT_USER_EMAIL, resultContent.userName); Preferences.Set(Consts.CURRENT_USER_PROFILE_ID, resultContent.userProfileId); Analytics.TrackEvent("Successful Login", new Dictionary <string, string> { { "user", user } }); return(true); } else { AuthenticationHelper.ClearSecureStorageAuthValues(); Preferences.Set(Consts.CURRENT_ASP_USER_ID, string.Empty); Preferences.Remove(Consts.CURRENT_USER_EMAIL); Preferences.Set(Consts.CURRENT_USER_PROFILE_ID, 0); Analytics.TrackEvent("Unsuccessful Login", new Dictionary <string, string> { { "user", user } }); return(false); } } catch (Exception ex) { Debug.WriteLine(@"ERROR {0}", ex.Message); Crashes.TrackError(ex); return(false); } }
public void AuthRemoveToken() { try { AuthenticationHelper.ClearSecureStorageAuthValues(); Preferences.Set(Consts.CURRENT_ASP_USER_ID, string.Empty); Preferences.Remove(Consts.CURRENT_USER_EMAIL); Preferences.Set(Consts.CURRENT_USER_PROFILE_ID, 0); Analytics.TrackEvent("Successfully Logged Out"); } catch (Exception ex) { Debug.WriteLine(@"ERROR {0}", ex.Message); Crashes.TrackError(ex); } }