Пример #1
0
 public static bool ShouldRefresh(this TokenInfoDTO token)
 {
     if (DateTime.Now.Ticks - token.StoredDate >= token.expiresIn.Ticks - token.StoredDate)
     {
         return(true);
     }
     return(false);
 }
Пример #2
0
        public async Task <TokenInfoDTO> CheckApiCredentialsAsync(string user, string pass)
        {
            Uri    uri  = new Uri(string.Format(App.ApiUrl + App.ApiAppName + "/Users/authenticate", string.Empty));
            string json = JsonConvert.SerializeObject(new UserDTO {
                username = user, password = pass
            });
            StringContent content = new StringContent(json, Encoding.UTF8, "application/json");

            HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Post, uri);

            httpRequest.Content = content;
            IEnumerable <string> cookieStrings = null;

            var response = await _client.SendAsync(httpRequest);

            response.Headers.TryGetValues("refreshToken", out cookieStrings);
            IEnumerable <Cookie> responseCookies = _cookies.GetCookies(uri).Cast <Cookie>();

            foreach (Cookie cookie in responseCookies)
            {
                _cookies.Add(uri, cookie);
            }

            if (response.IsSuccessStatusCode)
            {
                var          result      = response.Content.ReadAsStringAsync().Result;
                TokenInfoDTO userInfoDTO = JsonConvert.DeserializeObject <TokenInfoDTO>(result);
                if (!string.IsNullOrEmpty(userInfoDTO.jwtToken))
                {
                    //questa scadenza vale solo per il token di refresh
                    string decodeRefreshToken = HttpUtility.UrlDecode(responseCookies.Select(c => c.Value).FirstOrDefault());

                    userInfoDTO.refreshToken        = decodeRefreshToken;
                    userInfoDTO.ExpiresRefreshToken = responseCookies.Select(c => c.Expires.Ticks).FirstOrDefault();
                    userInfoDTO.StoredDate          = DateTime.Now.Ticks;
                    App.Token = userInfoDTO;
                    return(userInfoDTO);
                }
            }
            else if (response.StatusCode == HttpStatusCode.BadRequest)
            {
                MessagingCenter.Send <string, string>(MessagingCenterEvents.Subscriber, MessagingCenterEvents.AlertError, Messages.Login.ApiUserNotFound);
            }
            else
            {
                MessagingCenter.Send <string, string>(MessagingCenterEvents.Subscriber, MessagingCenterEvents.AlertError, Messages.Login.UnknownApiCredentialsCheck);
            }
            return(null);
        }
Пример #3
0
        public async Task RefreshTokenAsync(int userId)
        {
            Uri    uri  = new Uri(string.Format(App.ApiUrl + App.ApiAppName + "/Users/refresh-token", string.Empty));
            string json = string.Empty;

            try
            {
                if (userId > 0)
                {
                    json = JsonConvert.SerializeObject(userId);
                }
                StringContent      content     = new StringContent(json, Encoding.UTF8, "application/json");
                HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Post, uri);
                httpRequest.Content = content;

                _handler.CookieContainer.Add(new Cookie("refreshToken", App.Token.refreshToken, "/", uri.Host));


                var response = await _client.SendAsync(httpRequest);

                if (response.IsSuccessStatusCode)
                {
                    var result = response.Content.ReadAsStringAsync().Result;
                    IEnumerable <Cookie> responseCookies = _cookies.GetCookies(uri).Cast <Cookie>();
                    TokenInfoDTO         refreshTokenDTO = JsonConvert.DeserializeObject <TokenInfoDTO>(result);
                    if (!string.IsNullOrEmpty(refreshTokenDTO.jwtToken))
                    {
                        string decodeRefreshToken = HttpUtility.UrlDecode(responseCookies.Select(c => c.Value).FirstOrDefault());

                        refreshTokenDTO.refreshToken        = decodeRefreshToken;
                        refreshTokenDTO.ExpiresRefreshToken = responseCookies.Select(c => c.Expires.Ticks).FirstOrDefault();
                        //refreshTokenDTO.expiresIn = new DateTime(refreshTokenDTO.ExpiresRefreshToken);
                        refreshTokenDTO.StoredDate = DateTime.Now.Ticks;
                        App.Token.jwtToken         = refreshTokenDTO.jwtToken;
                        App.Token = refreshTokenDTO;
                        await App.Current.SavePropertiesAsync();
                    }
                }
            }
            catch (Exception ecc)
            {
                Console.WriteLine(ecc.Message);
            }
        }
Пример #4
0
        public async Task Login()
        {
            stopwatch.Reset();
            try
            {
                IsBusy = true;
                if (HasInternetConnection)
                {
                    if (string.IsNullOrEmpty(UserName) || string.IsNullOrEmpty(Password))
                    {
                        MessagingCenter.Send <string, string>(MessagingCenterEvents.Subscriber, MessagingCenterEvents.AlertError, Messages.Login.InvalidCredentials);
                        IsBusy = false;
                        return;
                    }
                    if (!string.IsNullOrEmpty(UserName) && !string.IsNullOrEmpty(Password))
                    {
                        stopwatch.Start();
                        _userToken = await _apiService.CheckApiCredentialsAsync(UserName, Password);

                        stopwatch.Stop();
                        if (_userToken != null)
                        {
                            // qua andró a stampare un popup in caso di lavorazione rimaste pendenti.
                            await FinishLoginProcess();
                        }
                        else
                        {
                            IsBusy = false;
                            return;
                        }
                    }
                }
                else
                {
                    MessagingCenter.Send <string, string>(MessagingCenterEvents.Subscriber, MessagingCenterEvents.AlertError, Messages.NoInternetConnection);
                }
                IsBusy = false;
            }
            catch (Exception ecc)
            {
                Log.Error("AppOfficina", ecc.Message);
                IsBusy = false;
            }
        }