} //LoginAsync internal void Logout() { StopRefreshToken(); ApplicationSettings.Clear(); Token = new JwtAuthToken(); IsLoggedIn = false; }
private bool LoadTokenInfo(Response <Object> loginApiResponse) { JwtAuthToken authResult = new JwtAuthToken(); var stringToken = loginApiResponse.Result.ToString(); var parts = stringToken.Split('.'); var decoded = parts[1].Base64UrlDecode(); var sdecoded = Encoding.UTF8.GetString(decoded); var jsonToken = JsonConvert.DeserializeObject <Dictionary <string, object> >(sdecoded); #region Token-Parse foreach (var kvp in jsonToken) { switch (kvp.Key) { case "sub": authResult.UID = kvp.Value.ToString(); break; case "unique_name": authResult.UserName = kvp.Value.ToString(); break; case "jti": authResult.Jti = kvp.Value.ToString(); break; case "site": authResult.SiteId = kvp.Value.ToString(); break; case "ma": authResult.Ma = kvp.Value.ToString(); break; case "roles": { authResult.Roles = new List <string>(); if ((kvp.Value as IList) != null) { foreach (var rl in (IList)kvp.Value) { authResult.Roles.Add(rl.ToString()); } } else { authResult.Roles.Add(kvp.Value.ToString()); } } break; case "nbf": authResult.Nbf = kvp.Value.ToString(); break; case "exp": authResult.Exp = kvp.Value.ToString(); break; case "iss": authResult.Iss = kvp.Value.ToString(); break; case "aud": authResult.Aud = kvp.Value.ToString(); break; default: break; } // switch (kvp.Key... } // foreach (var kvp... #endregion authResult.AccessToken = stringToken; Token = authResult; return(true); }
private async Task <bool> TryLogin(string userName, string password) { bool result = false; Token = null; Rs = null; JwtAuthToken authResult = new JwtAuthToken(); try { //ApplicationSettings.Clear(); var loginAPI = RestService.For <IAuthServiceApi>(ApiEndPoints.AUTH_SERVICE_URL); var stringToken = await loginAPI.PostCredentials(new UserLogin { UserName = userName, Password = password }); var parts = stringToken.Split('.'); var decoded = Base64UrlDecode(parts[1]); var sdecoded = Encoding.UTF8.GetString(decoded); var jsonToken = JsonConvert.DeserializeObject <Dictionary <string, object> >(sdecoded); #region Token-Parse foreach (var kvp in jsonToken) { switch (kvp.Key) { case "sub": authResult.UID = kvp.Value.ToString(); break; case "unique_name": authResult.UserName = kvp.Value.ToString(); break; case "jti": authResult.Jti = kvp.Value.ToString(); break; case "site": authResult.SiteId = kvp.Value.ToString(); break; case "ma": authResult.Ma = kvp.Value.ToString(); break; case "roles": { authResult.Roles = new List <string>(); if ((kvp.Value as IList) != null) { foreach (var rl in (IList)kvp.Value) { authResult.Roles.Add(rl.ToString()); } } else { authResult.Roles.Add(kvp.Value.ToString()); } } break; case "nbf": authResult.Nbf = kvp.Value.ToString(); break; case "exp": authResult.Exp = kvp.Value.ToString(); break; case "iss": authResult.Iss = kvp.Value.ToString(); break; case "aud": authResult.Aud = kvp.Value.ToString(); break; default: break; } // switch (kvp.Key... } // foreach (var kvp... #endregion authResult.AccessToken = stringToken; Token = authResult; #region llenar el common_ES Rs = new RefitSettings() { AuthorizationHeaderValueGetter = () => Task.FromResult(Token.AccessToken) }; var common_ES_Api = RestService.For <IAuthServiceApi>(ApiEndPoints.API_SERVICE_URL, Rs); var coomon_ES = await common_ES_Api.GetCommon_ES(); Token.CommonES = coomon_ES; #endregion ApplicationSettings.SelectedSiteId = coomon_ES.SiteId; ApplicationSettings.SelectedSiteName = coomon_ES.SiteName; ApplicationSettings.SelectedEleccionId = coomon_ES.EleccionId; ApplicationSettings.SelectedEleccionName = coomon_ES.EleccionName; ApplicationSettings.LastUserLogin = new UserLogin { UserName = userName, Password = password }; ApplicationSettings.LastAccessToken = Token.AccessToken; result = true; } catch (ApiException ex) { Debug.WriteLine(ex.ToString()); Debug.WriteLine(ex.StatusCode); } return(result); }