Esempio n. 1
0
        private async void OnSendTokenClicked(object obj)
        {
            if (SecureStorageHelper.CheckIfUserSessionIsActive().Result)
            {
                App.Current.MainPage = new NavigationPage(new ItemsPage());
            }
            if (string.IsNullOrEmpty(Token))
            {
                MessagingCenter.Send(this, "AuthError", "Token cannot be empty.");
                return;
            }


            IsBusy = true;

            ApiTwoFactorResponse apiResponse = await _apiService.TwoFactorLogIn(await SecureStorageHelper.GetUserId(), Token);

            IsBusy = false;

            if (apiResponse.VerificationStatus != 1)
            {
                if (apiResponse.VerificationStatus == 0)
                {
                    MessagingCenter.Send(this, "AuthError", apiResponse.Messages.First());
                    return;
                }
                else
                {
                    MessagingCenter.Send(this, "AuthError", "Your code has expired. Try log in again.");
                    SecureStorageHelper.ClearData();
                    await App.Current.MainPage.Navigation.PopModalAsync();

                    return;
                }
            }

            IsBusy = true;
            if (!_jwtHelper.ValidateToken(apiResponse.AccessToken, out _))
            {
                // indicate errors
                MessagingCenter.Send(this, "AuthError", "Json web token is invalid");
                await App.Current.MainPage.Navigation.PopModalAsync();

                return;
            }


            await SecureStorageHelper.SaveUserData(apiResponse);



            //App.Current.MainPage.Navigation.InsertPageBefore(new ItemsPage(), TwoFactorPage);
            // await Navigation.PopAsync();

            await App.Current.MainPage.Navigation.PopModalAsync();

            IsBusy = false;
            App.Current.MainPage = new NavigationPage(new ItemsPage());
        }
Esempio n. 2
0
        public async Task <IActionResult> TwoFactorLogIn(string id, TwoFactorViewModel model)
        {
            if (!ModelState.IsValid)
            {
                ModelState.AddModelError("Error", "Code can not be null.");
                ViewBag.AuthUserId = id;
                return(View(new TwoFactorViewModel()));
            }



            int idUser = Int32.Parse(dataProtectionHelper.Decrypt(id, "QueryStringsEncryptions"));


            ApiTwoFactorResponse apiResponse = await _apiService.TwoFactorLogIn(idUser, model.Token);


            if (apiResponse.VerificationStatus != 1)
            {
                if (apiResponse.VerificationStatus == 0)
                {
                    ViewBag.AuthUserId = id;
                    ModelState.AddModelError("Error", apiResponse.Messages.First());

                    return(View(new TwoFactorViewModel()));
                }
                else
                {
                    TempData["logoutMessage"] = "Your code has expired. Try log in again.";
                    _encryptionService.RemoveEncryptionKey(idUser.ToString());
                    return(RedirectToAction(controllerName: "Home", actionName: "Index"));
                }
            }


            ClaimsPrincipal          claimsPrincipal;
            AuthenticationProperties authProperties;
            var isSuccess = _jwtHelper.ValidateToken(apiResponse.AccessToken, out claimsPrincipal, out authProperties);

            if (!isSuccess)
            {
                ViewBag.AuthUserId = id;
                ModelState.AddModelError("Error", "Token is invalid.");

                return(View(new TwoFactorViewModel()));
            }
            await _logInHandler.LogInUser(claimsPrincipal, authProperties);

            return(RedirectToAction("Index", "Wallet"));
        }