partial void LoginButton_TouchUpInside(UIButton sender)
        {
            PasswordManager PM = new PasswordManager();

            using (var connection = new SQLite.SQLiteConnection(pathToDatabase))
            {
                var query = connection.Table <User>();
                foreach (User user in query)
                {
                    UserList.Add(user);
                }

                //check if user existed in the database


                if (gothroughDatabaseByUsername(UserList, UsernameText.Text) != null)

                {
                    User selectedUser = gothroughDatabaseByUsername(UserList, UsernameText.Text);
                    //check if user typed match database account

                    if (PM.IsPasswordMatch(PasswordText.Text, selectedUser.OwnerSalt, selectedUser.HashedPassword))
                    {
                        UserPageController UPC = this.Storyboard.InstantiateViewController("userpagecontroller") as UserPageController;
                        NavigationController.PushViewController(UPC, true);
                        UPC.inAccountName = "Welcome" + UsernameText.Text;
                    }

                    //if account existed , password not match
                    else
                    {
                        var alert = UIAlertController.Create("Password Incorrect ", "Please input correct password", UIAlertControllerStyle.Alert);
                        alert.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, null));
                        PresentViewController(alert, true, null);
                    }
                }
                else
                {
                    var alert = UIAlertController.Create("Username does not exist", "Please input correct Username and password to login", UIAlertControllerStyle.Alert);
                    alert.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, null));
                    PresentViewController(alert, true, null);
                }
            }
        }
Пример #2
0
        partial void SignUpButton_TouchUpInside(UIButton sender)
        {
            using (var connection = new SQLite.SQLiteConnection(pathToDatabase))
            {
                /*
                 * check if password match
                 */
                //if password match
                if ((PasswordText.Text).Equals(ConfirmPasswordText.Text))
                {
                    //check if entered Username existed
                    if (!isAccountExisted(UsernameText.Text, pathToDatabase))
                    {
                        /*
                         * Hash and salt the password
                         */

                        PasswordManager PM        = new PasswordManager();
                        string          salt      = SaltGenerator.GetSaltString();
                        string          HashedPwd = PM.GeneratePasswordHash(PasswordText.Text, out salt);
                        User            newuser   = new User();
                        newuser.Username       = UsernameText.Text;
                        newuser.HashedPassword = HashedPwd;
                        newuser.OwnerSalt      = salt;
                        newuser.Email          = EmailText.Text;

                        /*
                         * Check if User type valid
                         */
                        //Username should more than 5 charactors
                        if (newuser.Username.Length > 5)
                        {
                            //Password should contain number and upper & lower case
                            if (CheckPassword(PasswordText.Text))
                            {
                                //Email should in the correct format
                                if (CheckEmailValidation(newuser.Email))
                                {
                                    connection.Insert(newuser);
                                    //After login successfully , auto go to the userpage controller

                                    UserPageController UPC = this.Storyboard.InstantiateViewController("userpagecontroller") as UserPageController;
                                    NavigationController.PushViewController(UPC, true);
                                    UPC.inAccountName = "Welcome" + UsernameText.Text;
                                    var alert = UIAlertController.Create("Successful", "You have been signed up ", UIAlertControllerStyle.Alert);
                                    alert.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, null));
                                    PresentViewController(alert, true, null);
                                }
                                else
                                {
                                    var alert = UIAlertController.Create("Email format not correct", "Please type a valid email address", UIAlertControllerStyle.Alert);
                                    alert.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, null));
                                    PresentViewController(alert, true, null);
                                }
                            }
                            else
                            {
                                var alert = UIAlertController.Create("Password format incorrect", "Password should contain number , upper and lower case", UIAlertControllerStyle.Alert);
                                alert.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, null));
                                PresentViewController(alert, true, null);
                            }
                        }
                        else
                        {
                            var alert = UIAlertController.Create("Username format incorrect", "Username should have more than 5 charactor", UIAlertControllerStyle.Alert);
                            alert.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, null));
                            PresentViewController(alert, true, null);
                        }
                    }

                    /*
                     * if User existed
                     */
                    else
                    {
                        var alert = UIAlertController.Create("Username already used", "Please use a new one", UIAlertControllerStyle.Alert);
                        alert.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, null));
                        PresentViewController(alert, true, null);
                    }
                }

                //password not match
                else
                {
                    var alert = UIAlertController.Create("Password not match", "Please make sure ConfirmPassword match the password", UIAlertControllerStyle.Alert);
                    alert.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, null));
                    PresentViewController(alert, true, null);
                }
            }
        }