Пример #1
0
        public bool UpdatePassWord(string accountNumber)
        {
            var account = _accountModel.GetAccountByAccountNumber(accountNumber);

            Console.WriteLine("Nhập mật khẩu cũ của bạn: ");
            string oldPassWord = PromptHelper.GetPassword();

            // mã hóa pass người dùng nhập vào kèm theo muối trong database và so sánh kết quả với password đã được mã hóa trong database.
            if (account != null && _passwordHelper.ComparePassword(oldPassWord, account.Salt, account.PasswordHash))
            {
                string newPassword;
                while (true)
                {
                    Console.WriteLine("nhập mật khẩu mới");
                    newPassword = PromptHelper.GetPassword();
                    if (!ValidateHelper.IsPasswordValid(newPassword))
                    {
                        Console.WriteLine("Mật khẩu không hợp lệ mời nhập lại");
                    }
                    else if (oldPassWord.Equals(newPassword))
                    {
                        Console.WriteLine("Bạn đã nhập mật khẩu cũ mời nhập lại");
                    }
                    else
                    {
                        break;
                    }
                }

                //confirm new password
                Console.WriteLine("Hãy nhập lại mật khẩu để xác nhận");
                string confirmNewPassword = "";
                while (true)
                {
                    confirmNewPassword = PromptHelper.GetPassword();
                    if (confirmNewPassword.Equals(newPassword))
                    {
                        break;
                    }

                    Console.WriteLine("Mật khẩu không khớp, hãy nhập lại");
                }

                //update md5hash
                string newHashPassWord = _passwordHelper.MD5Hash(newPassword + account.Salt);
                var    updateSuccess   = _accountModel.UpdateAccountByAccountNumber(accountNumber, "hashPassword", newHashPassWord);
                if (updateSuccess)
                {
                    Console.WriteLine("Đã cập nhật mật khẩu thành công");
                    return(true);
                }
                return(false);
            }

            Console.WriteLine("Bạn đã nhập sai mật khẩu");
            return(false);
        }
Пример #2
0
        public Account Login()
        {
            Console.WriteLine("Nhập tên tài khoản: ");
            var username = Console.ReadLine();

            Console.WriteLine("Nhập mật khẩu: ");
            var password = PromptHelper.GetPassword();
            var account  = _accountModel.GetActiveAccountByUsername(username);

            // mã hóa pass người dùng nhập vào kèm theo muối trong database và so sánh kết quả với password đã được mã hóa trong database.
            if (account != null && _passwordHelper.ComparePassword(password, account.Salt, account.PasswordHash))
            {
                Console.WriteLine("Đăng nhập thành công");
                return(account);
            }

            return(null);
        }
Пример #3
0
        public Account AddUser() // cho phép thêm tài khoản khách hoặc admin
        {
            var accountNumber = "";

            while (true)
            {
                accountNumber = AccountHelper.RandomAccountNumber(15);
                var isExist = _accountModel.CheckExistAccountNumber(accountNumber);
                if (isExist == null)
                {
                    Console.WriteLine("Kiểm tra kết nối của bạn");
                    break;
                }

                if (isExist == false)
                {
                    break;
                }
            }

            //init account obj
            var newAccount = new Account()
            {
                Balance       = 0,
                Status        = AccountStatus.ACTIVE,
                Salt          = _passwordHelper.GenerateSalt(),
                AccountNumber = accountNumber,
            };

            //get Role
            Console.WriteLine("Bạn muốn tạo tài khoản thường hay tài khoản cho admin ?");
            Console.WriteLine("1. Tài khoản thường");
            Console.WriteLine("2. Tài khoản admin");
            Console.WriteLine("Chọn 1 hoặc 2");
            var choice = PromptHelper.GetUserChoice(1, 2);

            switch (choice)
            {
            case 1:
                newAccount.Role = AccountRole.GUEST;
                break;

            case 2:
                newAccount.Role = AccountRole.ADMIN;
                break;
            }

            //get full Name
            Console.WriteLine("Nhập tên đầy đủ: ");
            string fullName = Console.ReadLine();

            newAccount.FullName = fullName;
            //get email
            Console.WriteLine("Nhập email: ");
            string email;

            while (true)
            {
                email = Console.ReadLine();
                if (ValidateHelper.IsEmailValid(email))
                {
                    break;
                }

                Console.WriteLine("Email không hợp lệ mời nhập lại");
            }

            newAccount.Email = email;
            //get phone number
            Console.WriteLine("Nhập số điện thoại: ");
            string phoneNumber;

            while (true)
            {
                phoneNumber = Console.ReadLine();
                if (ValidateHelper.IsPhoneNumberValid(phoneNumber))
                {
                    break;
                }

                Console.WriteLine("Số điện thoại không hợp lệ mời nhập lại");
            }

            newAccount.PhoneNumber = phoneNumber;
            //get username
            Console.WriteLine("Nhập tên đăng nhập: ");
            //prompt for username if exsist -> prompt again
            string username;

            while (true)
            {
                username = Console.ReadLine();
                var isValid = ValidateHelper.IsUsernameValid(username);
                var isExist = _accountModel.CheckExistAccountByUsername(username);
                if (isExist == null)
                {
                    Console.WriteLine("Hãy kiểm tra kết nối của bạn");
                    return(null);
                }
                if (isValid && isExist == false)
                {
                    break;
                }

                if (!isValid)
                {
                    Console.WriteLine("tên đăng nhập không hợp lệ mời nhập lại");
                    continue;
                }

                if (isExist == true)
                {
                    Console.WriteLine("Tên đăng nhập đã tồn tại mời nhập lại");
                    continue;
                }
            }

            newAccount.Username = username;
            //get password
            Console.WriteLine("Hãy nhập vào mật khẩu:");
            string password;

            while (true)
            {
                password = PromptHelper.GetPassword();
                if (ValidateHelper.IsPasswordValid(password))
                {
                    break;
                }

                Console.WriteLine("Mật khẩu không hợp lệ mời nhập lại");
            }

            //xác nhận mật khẩu
            Console.WriteLine("Nhập lại mật khẩu của bạn để xác nhận: ");
            string confirmPass = "";

            while (true)
            {
                confirmPass = PromptHelper.GetPassword();
                if (confirmPass.Equals(password))
                {
                    break;
                }

                Console.WriteLine("Mật khẩu không khớp, mời nhập lại");
            }


            //hash pwd and salt to get password hashed
            newAccount.PasswordHash = _passwordHelper.MD5Hash(password + newAccount.Salt);

            var result = _accountModel.SaveAccount(newAccount);

            if (result == false)
            {
                return(null);
            }

            return(newAccount);
        }
Пример #4
0
        public Account Register()
        {
            var accountNumber = "";

            while (true)
            {
                accountNumber = AccountHelper.RandomAccountNumber(15);
                var isExist = _accountModel.CheckExistAccountNumber(accountNumber);
                if (isExist == null)
                {
                    Console.WriteLine("Hãy kiểm tra lại kết nối của bạn");
                    return(null);
                }

                if (isExist == false)
                {
                    break;
                }
            }

            //init account obj
            var newAccount = new Account()
            {
                Balance       = 0,
                Status        = AccountStatus.ACTIVE,
                Salt          = _passwordHelper.GenerateSalt(),
                AccountNumber = accountNumber,
                Role          = AccountRole.GUEST
            };

            //get full Name
            Console.WriteLine("Nhập tên đầy đủ: ");
            string fullName = Console.ReadLine();

            newAccount.FullName = fullName;
            //get email
            Console.WriteLine("Nhập email của bạn: ");
            string email;

            while (true)
            {
                email = Console.ReadLine();
                if (ValidateHelper.IsEmailValid(email))
                {
                    break;
                }

                Console.WriteLine("Email không hợp lệ mời nhập lại");
            }

            newAccount.Email = email;
            //get phone number
            Console.WriteLine("Nhập số điện thoại: ");
            string phoneNumber;

            while (true)
            {
                phoneNumber = Console.ReadLine();
                if (ValidateHelper.IsPhoneNumberValid(phoneNumber))
                {
                    break;
                }

                Console.WriteLine("Số điện thoại không hợp lệ mời nhập lại");
            }

            newAccount.PhoneNumber = phoneNumber;
            //get username
            Console.WriteLine("Nhập tên đăng nhập: ");
            //prompt for username if exsist -> prompt again
            string username;

            while (true)
            {
                username = Console.ReadLine();
                var isValid = ValidateHelper.IsUsernameValid(username);
                var isExist = _accountModel.CheckExistAccountByUsername(username);
                if (isExist == null)
                {
                    Console.WriteLine("Hãy kiểm tra lại kết nối của bạn");
                    return(null);
                }

                if (isExist == false && isValid)
                {
                    break;
                }
                if (isExist == true)
                {
                    Console.WriteLine("Tên đăng nhập đã tồn tại");
                    continue;
                }

                if (isValid == false)
                {
                    Console.WriteLine("Tên đăng nhập không hợp lệ ");
                    continue;
                }
            }

            newAccount.Username = username;
            //get password
            Console.WriteLine("Hãy nhập vào mật khẩu của bạn");
            string password;

            while (true)
            {
                password = PromptHelper.GetPassword();
                if (ValidateHelper.IsPasswordValid(password))
                {
                    break;
                }

                Console.WriteLine("Mật khẩu không hợp lệ mời nhập lại");
            }

            //xác nhận mật khẩu
            Console.WriteLine("Nhập lại mật khẩu của bạn để xác nhận: ");
            string confirmPass = "";

            while (true)
            {
                confirmPass = PromptHelper.GetPassword();
                if (confirmPass.Equals(password))
                {
                    break;
                }

                Console.WriteLine("Mật khẩu không khớp, mời nhập lại");
            }


            //hash pwd and salt to get password hashed
            newAccount.PasswordHash = _passwordHelper.MD5Hash(password + newAccount.Salt);

            var result = _accountModel.SaveAccount(newAccount);

            if (result == false)
            {
                return(null);
            }

            return(newAccount);
        }