Ejemplo n.º 1
0
        public LoginPageViewModel()
        {
            using (var con = DependencyService.Get <IDatabase>().GetConnection(ConnectionType.Login))
            {
                if (con.Table <User>().OrderByDescending(each => each.LastLoggedInDate).FirstOrDefault(each => true) is User potentialUser)
                {
                    Username = potentialUser.Username;
                }
            }
            LoginCommand = new Command(async() =>
            {
                if (string.IsNullOrWhiteSpace(Username))
                {
                    Error = "Kullanıcı adı boş olamaz";
                    UI.DisplayCustomAlert(nameof(Username));
                    return;
                }
                else
                {
                    if (string.IsNullOrWhiteSpace(Password))
                    {
                        Error = "Şifre boş olamaz";
                        UI.DisplayCustomAlert(nameof(Password));
                        return;
                    }
                }

                Utility.ShowIndicator = true;

                EllipticCurve elliptic = new EllipticCurve(false);

                KeyValuePair <byte[], EstablishSharedSecretResponseModel> response = default(KeyValuePair <byte[], EstablishSharedSecretResponseModel>);

                await Task.Run(() => response = elliptic.KeyExchange(Username));

                if (elliptic.IsSucceeded)
                {
                    Utility.SharedSecret = elliptic.SharedSecret;

                    var parameters = new
                    {
                        Password
                    };

                    var serializedParameters = JsonConvert.SerializeObject(parameters);

                    var loginResponse = await Helper.httpPostAsync($"{Utility.BaseURL}/api/user/login?username={Username}", serializedParameters, IsSecure: true);

                    Utility.ShowIndicator = false;

                    try
                    {
                        var plainLoginResponse = JsonConvert.DeserializeObject <LoginResponseModel>(loginResponse);

                        if (plainLoginResponse.responseVal == 0)
                        {
                            Password = "";
                            using (var con = DependencyService.Get <IDatabase>().GetConnection(ConnectionType.Login))
                            {
                                var bufferUser        = con.Table <User>().FirstOrDefault(each => each.accessToken == plainLoginResponse.accessToken);
                                string connectedHomes = Helper.ConvertCollectionToString(plainLoginResponse.ConnectedHomes, ',');

                                if (bufferUser != null)
                                {
                                    con.BeginTransaction();
                                    bufferUser.LoggedIn         = true;
                                    bufferUser.UserType         = plainLoginResponse.userType;
                                    bufferUser.UserNameSurname  = plainLoginResponse.nameSurname;
                                    bufferUser.ConnectedHomes   = connectedHomes;
                                    bufferUser.CurrentHome      = plainLoginResponse.CurrentHome;
                                    bufferUser.LastLoggedInDate = DateTime.Now;
                                    con.Update(bufferUser);
                                    con.Commit();
                                    Utility.User = bufferUser;
                                }
                                else
                                {
                                    var user = new User()
                                    {
                                        accessToken      = plainLoginResponse.accessToken,
                                        LastLoggedInDate = DateTime.Now,
                                        ConnectedHomes   = connectedHomes,
                                        LoggedIn         = true,
                                        CurrentHome      = plainLoginResponse.CurrentHome,
                                        Username         = Username,
                                        UserNameSurname  = plainLoginResponse.nameSurname,
                                        UserType         = plainLoginResponse.userType
                                    };
                                    con.Insert(user);
                                    Utility.User = user;
                                }
                            }
                            MainPage.IsGestureEnabled = true;
                            await MainPage.NavigateFromMenu((int)MenuItemType.Home);
                        }
                        else
                        {
                            Error = plainLoginResponse.responseText;
                            UI.DisplayCustomAlert(nameof(Username));
                        }
                    }
                    catch (Exception e)
                    {
                        await UI.DisplayAlert("Homem8", e.Message, "Tamam");
                    }
                }
                else
                {
                    Utility.ShowIndicator = false;
                    Error = response.Value.responseText;
                    UI.DisplayCustomAlert(nameof(Username));
                }
            });
        }
Ejemplo n.º 2
0
        public ForgotPasswordPageViewModel()
        {
            NewPasswordCommand = new Command(async() =>
            {
                if (!string.IsNullOrWhiteSpace(NewPassword) && !string.IsNullOrWhiteSpace(NewPassword2))
                {
                    if (NewPassword == NewPassword2)
                    {
                        Utility.ShowIndicator = true;

                        AESCrypt AES = new AESCrypt(sharedSecret, new byte[16]);

                        var cipheredParameters = AES.Encrypt(JsonConvert.SerializeObject(new
                        {
                            NewPassword,
                            ValidationCode
                        }));

                        var newPasswordResponse = JsonConvert.DeserializeObject <BaseResponseModel>(
                            AES.Decrypt(await Helper.httpPostAsync($"{Utility.BaseURL}/api/user/SetNewPassword?username={Username}", cipheredParameters)));

                        if (newPasswordResponse.responseVal == 0)
                        {
                            Utility.SharedSecret = sharedSecret;

                            var serializedParameters = JsonConvert.SerializeObject(new { password = NewPassword });

                            var loginResponse = JsonConvert.DeserializeObject <LoginResponseModel>(
                                await Helper.httpPostAsync($"{Utility.BaseURL}/api/user/login?username={Username}", serializedParameters, IsSecure: true));

                            if (loginResponse.responseVal == 0)
                            {
                                using (var con = DependencyService.Get <IDatabase>().GetConnection(ConnectionType.Login))
                                {
                                    var bufferUser        = con.Table <User>().FirstOrDefault(each => each.accessToken == loginResponse.accessToken);
                                    string connectedHomes = Helper.ConvertCollectionToString(loginResponse.ConnectedHomes, ',');

                                    if (bufferUser != null)
                                    {
                                        con.BeginTransaction();
                                        bufferUser.LoggedIn         = true;
                                        bufferUser.Username         = Username;
                                        bufferUser.UserNameSurname  = loginResponse.nameSurname;
                                        bufferUser.ConnectedHomes   = connectedHomes;
                                        bufferUser.CurrentHome      = loginResponse.CurrentHome;
                                        bufferUser.LastLoggedInDate = DateTime.Now;
                                        con.Update(bufferUser);
                                        con.Commit();
                                        Utility.User = bufferUser;
                                    }
                                    else
                                    {
                                        User user = new User()
                                        {
                                            accessToken      = loginResponse.accessToken,
                                            LastLoggedInDate = DateTime.Now,
                                            LoggedIn         = true,
                                            Username         = Username,
                                            CurrentHome      = loginResponse.CurrentHome,
                                            ConnectedHomes   = connectedHomes,
                                            UserType         = loginResponse.userType,
                                            UserNameSurname  = loginResponse.nameSurname
                                        };
                                        con.Insert(user);
                                        Utility.User = user;
                                    }
                                }
                                RootPage.IsGestureEnabled = true;
                                await RootPage.NavigateFromMenu((int)MenuItemType.Home);
                            }
                            else
                            {
                                UI.DisplayCustomAlert(new KeyValuePair <string, string>("Pass", loginResponse.responseText));
                            }
                        }
                        else
                        {
                            UI.DisplayCustomAlert(new KeyValuePair <string, string>("Pass", newPasswordResponse.responseText));
                        }
                        Utility.ShowIndicator = false;
                    }
                    else
                    {
                        UI.DisplayCustomAlert(new KeyValuePair <string, string>("Pass", "Parolarınız aynı değil"));
                    }
                }
                else
                {
                    UI.DisplayCustomAlert(new KeyValuePair <string, string>("Pass", "Parola alanı boş bırakılamaz"));
                }
            });

            SendValidationCommand = new Command(async() =>
            {
                int mValidationCode = 0;
                bool error          = false;

                try
                {
                    mValidationCode = Convert.ToInt32(ValidationCode);
                }
                catch { error = true; }

                if (!string.IsNullOrWhiteSpace(ValidationCode))
                {
                    if (!error)
                    {
                        if (mValidationCode > 99999)
                        {
                            Utility.ShowIndicator = true;

                            EllipticCurve elliptic = new EllipticCurve(false);

                            await Task.Run(() =>
                            {
                                elliptic.KeyExchange(Username);
                            });

                            if (elliptic.IsSucceeded)
                            {
                                sharedSecret = elliptic.SharedSecret;

                                AESCrypt AES = new AESCrypt(sharedSecret, new byte[16]);

                                var cipheredParameters = AES.Encrypt(JsonConvert.SerializeObject(new
                                {
                                    ValidationCode = mValidationCode
                                }));

                                var validationVCodeResponse = JsonConvert.DeserializeObject <BaseResponseModel>(
                                    AES.Decrypt(await Helper.httpPostAsync($"{Utility.BaseURL}/api/user/ValidateVCode?username={Username}", cipheredParameters)));

                                Utility.ShowIndicator = false;

                                if (validationVCodeResponse.responseVal == 0)
                                {
                                    UI.CallNewPasswordStackLayout();
                                }
                                else
                                {
                                    UI.DisplayCustomAlert(new KeyValuePair <string, string>("Validation", validationVCodeResponse.responseText));
                                }
                            }
                        }
                        else
                        {
                            UI.DisplayCustomAlert(new KeyValuePair <string, string>("Validation", "Doğrulama kodu 6 haneli olmalıdır"));
                        }
                    }
                    else
                    {
                        UI.DisplayCustomAlert(new KeyValuePair <string, string>("Validation", "Doğrulama kodu sadece sayı olmalıdır"));
                    }
                }
                else
                {
                    UI.DisplayCustomAlert(new KeyValuePair <string, string>("Validation", "Doğrulama kodu boş bırakılamaz"));
                }
            });

            SendCommand = new Command(async() =>
            {
                if (!string.IsNullOrWhiteSpace(Username))
                {
                    if (Username.Length > 3)
                    {
                        Utility.ShowIndicator = true;

                        var validationResponse = JsonConvert.DeserializeObject <BaseResponseModel>(await Helper.httpGetAsync($"{Utility.BaseURL}/api/user/GetValidationCode?username={Username}"));

                        Utility.ShowIndicator = false;

                        if (validationResponse.responseVal == 0)
                        {
                            UI.CallValidationStackLayout();
                        }
                        else
                        {
                            UI.DisplayCustomAlert(new KeyValuePair <string, string>("User", validationResponse.responseText));
                        }
                    }
                    else
                    {
                        UI.DisplayCustomAlert(new KeyValuePair <string, string>("User", "Kullanıcı adı 4 karakterden uzun olmalıdır"));
                    }
                }
                else
                {
                    UI.DisplayCustomAlert(new KeyValuePair <string, string>("User", "Kullanıcı adı boş bırakılamaz"));
                }
            });
        }
Ejemplo n.º 3
0
        public RegisterPageViewModel()
        {
            ButtonText      = AppConfigurationModel.Instance.RegisterPageContent.ButtonText1;
            RegisterCommand = new Command(async() =>
            {
                switch (CurrentPage)
                {
                case 0:

                    if (!IsEmailValidationRunning)
                    {
                        if (ValidEmail)
                        {
                            var returnVal = ControlPageValues();

                            if (returnVal.Key == "Ok")
                            {
                                CurrentPage = 1;
                                UI.CallNextPage();
                                ButtonText = AppConfigurationModel.Instance.RegisterPageContent.ButtonText2;
                            }
                            else
                            {
                                UI.DisplayCustomAlert(returnVal);
                            }
                        }
                        else
                        {
                            UI.TriggerInvalidEmailAnimation();
                        }
                    }
                    else
                    {
                        await PopupNavigation.Instance.PushAsync(new ErrorPopup("Lütfen Bekleyiniz"));
                    }
                    break;

                case 1:

                    if (!IsUsernameValidationRunning)
                    {
                        if (ValidUsername)
                        {
                            var returnVal2 = ControlSecondPageValues();

                            if (returnVal2.Key == "Ok")
                            {
                                string errorLog = null;

                                Utility.ShowIndicator = true;

                                await Task.Run(() =>
                                {
                                    EllipticCurve elliptic = new EllipticCurve(true);

                                    var buffer = elliptic.KeyExchange(Username);

                                    if (elliptic.IsSucceeded)
                                    {
                                        Utility.SharedSecret = elliptic.SharedSecret;

                                        var serializedRegisterParameters = JsonConvert.SerializeObject(new
                                        {
                                            Email,
                                            Password,
                                            NameSurname,
                                            phoneNumber = PhoneNumber,
                                        });

                                        try
                                        {
                                            var response = JsonConvert.DeserializeObject <BaseResponseModel>(
                                                Helper.httpPostAsync($"{Utility.BaseURL}/api/user/register?username={Username}", serializedRegisterParameters, IsSecure: true).Result);

                                            if (response.responseVal == 0)
                                            {
                                                var serializedLoginParameters = JsonConvert.SerializeObject(new { password = Password });

                                                var loginResponse = JsonConvert.DeserializeObject <LoginResponseModel>(
                                                    Helper.httpPostAsync($"{Utility.BaseURL}/api/user/login?username={Username}", serializedLoginParameters, IsSecure: true).Result);

                                                if (loginResponse.responseVal == 0)
                                                {
                                                    Utility.ShowIndicator = false;
                                                    using (var con = DependencyService.Get <IDatabase>().GetConnection(ConnectionType.Login))
                                                    {
                                                        User bufferUser = new User()
                                                        {
                                                            accessToken      = loginResponse.accessToken,
                                                            LastLoggedInDate = DateTime.Now,
                                                            ConnectedHomes   = Helper.ConvertCollectionToString(loginResponse.ConnectedHomes, ','),
                                                            CurrentHome      = loginResponse.CurrentHome,
                                                            LoggedIn         = true,
                                                            Username         = Username,
                                                            UserType         = loginResponse.userType,
                                                            UserNameSurname  = loginResponse.nameSurname
                                                        };
                                                        con.Insert(bufferUser);
                                                        Utility.User = bufferUser;
                                                    }
                                                }
                                                else
                                                {
                                                    errorLog = loginResponse.responseText;
                                                }
                                            }
                                            else
                                            {
                                                errorLog = response.responseText;
                                            }
                                        }
                                        catch (Exception)
                                        {
                                            errorLog = Utility.UnexpectedMessage;
                                        }
                                    }
                                    else
                                    {
                                        errorLog = elliptic.Response.responseText;
                                    }
                                });

                                Utility.ShowIndicator = false;

                                if (errorLog != null)
                                {
                                    UI.DisplayCustomAlert(new KeyValuePair <string, string>(errorLog, null), true);
                                }
                                else
                                {
                                    RootPage.IsGestureEnabled = true;
                                    await RootPage.NavigateFromMenu((int)MenuItemType.Home);
                                }
                            }
                            else
                            {
                                UI.DisplayCustomAlert(returnVal2);
                            }
                        }
                        else
                        {
                            UI.TriggerInvalidUsernameAnimation();
                        }
                    }
                    else
                    {
                        await PopupNavigation.Instance.PushAsync(new ErrorPopup("Lütfen bekleyiniz."));
                    }
                    break;
                }
            });
        }