Beispiel #1
0
        public static async Task <bool> Login(string email, string password)
        {
            var login = new Login()
            {
                Email    = email,
                Password = password
            };
            var httpClient = new HttpClient();
            var json       = JsonConvert.SerializeObject(login);
            var content    = new StringContent(json, Encoding.UTF8, "application/json");
            var response   = await httpClient.PostAsync(AppSettings.ApiUrl + "api/Accounts/Login", content);

            if (!response.IsSuccessStatusCode)
            {
                return(false);
            }
            var jsonResult = await response.Content.ReadAsStringAsync();

            var result = JsonConvert.DeserializeObject <Token>(jsonResult);

            Preferences.Set("accessToken", result.access_token);
            Preferences.Set("userId", result.user_Id);
            Preferences.Set("userName", result.user_name);
            Preferences.Set("tokenExpirationTime", result.expiration_Time);
            Preferences.Set("currentTime", UnixTime.GetCurrentTime());
            return(true);
        }
Beispiel #2
0
        public static async Task <bool> RefreshToken()
        {
            var formVariables = new List <KeyValuePair <string, string> >();

            formVariables.Add(new KeyValuePair <string, string>("grant_type", "refresh_token"));
            formVariables.Add(new KeyValuePair <string, string>("client_id", XeroSettings.clientID));
            formVariables.Add(new KeyValuePair <string, string>("refresh_token", Preferences.Get("RefreshToken", string.Empty)));

            HttpClient client = new HttpClient();

            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));

            var request = new HttpRequestMessage(HttpMethod.Post, @"https://identity.xero.com/connect/token");

            request.Content = new FormUrlEncodedContent(formVariables);

            var response = await client.SendAsync(request);

            if (!response.IsSuccessStatusCode)
            {
                return(false);
            }

            var token = JsonConvert.DeserializeObject <Token>(await response.Content.ReadAsStringAsync());

            Preferences.Set("AccessToken", token.access_token);
            Preferences.Set("ExpiresIn", token.expires_in);
            Preferences.Set("CurrentTime", UnixTime.GetCurrentTime());
            Preferences.Set("RefreshToken", token.refresh_token);

            DecodeAccessToken();
            return(true);
        }
Beispiel #3
0
        public static async Task <bool> Login(string email, string senha)
        {
            var login = new LoginModel()
            {
                Email = email,
                Senha = senha
            };
            var json    = JsonConvert.SerializeObject(login);
            var content = new StringContent(json, Encoding.UTF8, "application/json");
            //método que irá fazer a requisição post para nossa API
            var resposta = await httpClient.PostAsync(AppSettings.ApiUrl + "api/contas/login", content);

            if (!resposta.IsSuccessStatusCode)
            {
                return(false);
            }
            var jsonResult = await resposta.Content.ReadAsStringAsync();

            var result = JsonConvert.DeserializeObject <Token>(jsonResult);

            Preferences.Set("accessToken", result.access_token);
            Preferences.Set("userId", result.user_id);
            Preferences.Set("userNome", result.user_nome);
            Preferences.Set("userEmail", result.user_email);
            Preferences.Set("tokenExpirationTime", result.expiration_Time);
            Preferences.Set("currentTime", (int)UnixTime.GetCurrentTime());
            return(true);
        }
Beispiel #4
0
        public static async Task CheckTokenValidity()
        {
            var expirationTime = Preferences.Get("tokenExpirationTime", 0);

            Preferences.Set("currentTime", UnixTime.GetCurrentTime());
            var currentTime = Preferences.Get("currentTime", 0);

            if (expirationTime < currentTime)
            {
                var email    = Preferences.Get("email", string.Empty);
                var password = Preferences.Get("password", string.Empty);
                await ApiService.Login(email, password);
            }
        }
Beispiel #5
0
        public static async Task CheckToken()
        {
            int expTokenTime = Preferences.Get("TokenExpTime", 0);

            Preferences.Set("CurrneTime", UnixTime.GetCurrentTime());
            var currentTime = Preferences.Get("CurrneTime", 0);

            if (expTokenTime < currentTime)
            {
                string email = Preferences.Get("email", string.Empty);
                string pass  = Preferences.Get("pass", string.Empty);
                await ApiServices.LoginUser(email, pass);
            }
        }
Beispiel #6
0
        public static async void CheckTokenValidity()
        {
            var expirationTime = Preferences.Get("tokenExpirationTime", 0L);

            Preferences.Set("currentTime", UnixTime.GetCurrentTime());
            var now = Preferences.Get("currentTime", 0);

            if (expirationTime < now)
            {
                var email    = Preferences.Get("email", string.Empty);
                var password = Preferences.Get("password", string.Empty);
                var login    = new Login()
                {
                    email = email, password = password
                };
                await ApiServices.Login(login);
            }
        }
        public static async Task <bool> Login(string email, string password)
        {
            using (HttpClient httpClient = new HttpClient())
            {
                var login = new Login()
                {
                    Email    = email,
                    Password = password
                };

                var json = JsonConvert.SerializeObject(login);
                using (StringContent content = new StringContent(json, Encoding.UTF8, "application/json"))
                {
                    var response = await httpClient.PostAsync(string.Format("{0}{1}", new object[] { AppSettings.API_URL, "Accounts/Login" }), content);

                    if (!response.IsSuccessStatusCode)
                    {
                        return(false);
                    }

                    try
                    {
                        var jsonResult = await response.Content.ReadAsStringAsync();

                        var result = JsonConvert.DeserializeObject <Token>(jsonResult);
                        Preferences.Set("access_token", result.access_token);
                        Preferences.Set("user_id", result.user_Id);
                        Preferences.Set("user_name", result.user_name);
                        Preferences.Set("token_exp_time", result.expiration_Time);
                        Preferences.Set("current_time", UnixTime.GetCurrentTime());
                        return(true);
                    }
                    catch (System.Exception ex)
                    {
                        throw ex;
                    }
                }
            }
        }