Esempio n. 1
0
        public async Task <ActionResult <dynamic> > Authenticate([FromBody] AuthDto model)
        {
            try
            {
                var user = userRepository.GetUserLogin(model.Login.ToLower());

                if (user == null)
                {
                    return(NotFound(new { message = "User not found with this login" }));
                }

                var isEqualPass = BCryptService.PasswordCompare(model.Password, user.Password);

                if (!isEqualPass)
                {
                    return(NotFound(new { message = "Password incorrect" }));
                }

                var token = TokenService.GenerateToken(user);
                user.Password = "";
                return(new
                {
                    user = user,
                    token = token
                });
            }
            catch (Exception ex)
            {
                return(CatchError(ex, "Authentication user"));
            }
        }
Esempio n. 2
0
        public async Task <IActionResult> QuenMatKhau(QuenMatKhauRequest request)
        {
            int    result          = 0;
            string newPassword     = SendMailService.GenerateString();
            string newHashPassword = BCryptService.HashPassword(newPassword);

            result = await _userService.QuenMatKhau(request.tenDangNhap, request.email, newHashPassword);

            if (result == 1)
            {
                var           subject = "Xác nhận quên mật khẩu";
                StringBuilder body    = new StringBuilder();
                body.AppendFormat("Mật khẩu mới của bạn là: {0}", newPassword);

                try
                {
                    var message = SendMailService.InitEmailMessage(request.email, subject, body.ToString());
                    SendMailService.SendMail(message);
                    return(Ok(result));
                }
                catch (Exception ex)
                {
                    return(StatusCode(500));
                }
            }

            return(NotFound());
        }
Esempio n. 3
0
        public async Task <ActionResult <int> > DoiMatKhau(DoiMatKhauRequest request)
        {
            int    result       = 0;
            string hashPassword = _userService.GetPasswordByMaTk(request.maTaiKhoan);

            if (hashPassword != null)
            {
                bool checkPassword = BCryptService.CheckPassword(request.matKhau, hashPassword);

                if (checkPassword == true)
                {
                    string newPassword = BCryptService.HashPassword(request.matKhauMoi);
                    result = await _userService.DoiMatKhau(request.maTaiKhoan, newPassword);
                }
            }
            return(result);
        }
Esempio n. 4
0
        public async Task <ActionResult <int> > DangKyTaiKhoanKhachHang(DangKyRequest request)
        {
            var result = 0;

            string maTaiKhoan = GenerateMaTaiKhoan(request.Sdt);

            string password     = SendMailService.GenerateString();
            string hashPassword = BCryptService.HashPassword(password);
            string soTaiKhoan   = GenerateSoTaiKhoan(request.Sdt);

            TaiKhoanDangNhapBO taiKhoanDangNhap = new TaiKhoanDangNhapBO();

            taiKhoanDangNhap.MaTaiKhoan     = maTaiKhoan;
            taiKhoanDangNhap.TenDangNhap    = request.TenDangNhap;
            taiKhoanDangNhap.MatKhau        = hashPassword;
            taiKhoanDangNhap.IdLoaiTaiKhoan = 1;

            TaiKhoanKhachHangBO taiKhoanKhachHang = new TaiKhoanKhachHangBO();

            taiKhoanKhachHang.MaTk        = maTaiKhoan;
            taiKhoanKhachHang.Sdt         = request.Sdt;
            taiKhoanKhachHang.Email       = request.Email;
            taiKhoanKhachHang.SoTaiKhoan  = soTaiKhoan;
            taiKhoanKhachHang.TenTaiKhoan = request.HoTen;
            taiKhoanKhachHang.SoDu        = 0;

            var themTaiKhoanResult = _userService.ThemTaiKhoanDangNhap(taiKhoanDangNhap);

            if (themTaiKhoanResult.Result == 1)
            {
                // Send mail after create success
                var           subject = "Đăng ký tài khoản";
                StringBuilder body    = new StringBuilder();
                body.AppendFormat("Mật khẩu của bạn là: {0}", password);
                var message = SendMailService.InitEmailMessage(request.Email, subject, body.ToString());
                SendMailService.SendMail(message);

                result = _userService.ThemThongTinTaiKhoanKhachHang(taiKhoanKhachHang).Result;
                return(result);
            }
            else
            {
                return(StatusCode(500));
            }
        }
Esempio n. 5
0
        public async Task <IActionResult> DongTaiKhoanThanhToan(DongTaiKhoanThanhToanRequest request)
        {
            var hashPassword = _userService.GetPasswordByMaTk(request.MaTaiKhoan);
            var response     = false;

            if (hashPassword != null)
            {
                bool checkPassword = BCryptService.CheckPassword(request.MatKhau, hashPassword);

                if (checkPassword == true)
                {
                    var result = _userService.DongTaiKhoanThanhToan(request.MaTaiKhoan).Result;
                    if (result > 0)
                    {
                        response = true;
                    }
                }
            }

            return(Ok(new { response }));
        }
Esempio n. 6
0
        //[HttpPost]
        //[Produces("application/json")]
        //[Route("RefreshToken")]
        //public async Task<IActionResult> Refresh(string token, string refreshToken)
        //{
        //    var princial = GetPrincipalFromExpiredToken(token);
        //    var userName = princial.Claims.ToList()[0].Value;
        //    var user = _userService.GetUserByUserName(userName).Result;

        //    // Check current request token of user
        //    if (user == null || user.RefreshToken != refreshToken)
        //    {
        //        return BadRequest();
        //    }

        //    var newJwtToken = GenerateJSONWebToken(user);
        //    var newRefreshToken = GenerateRefreshToken();

        //    var result = _userService.EditUserRefreshToken(userName, refreshToken);

        //    return new ObjectResult(new
        //    {
        //        token = newJwtToken,
        //        refreshToken = newRefreshToken
        //    });
        //}

        //[Authorize]
        //[HttpPost]
        //[Produces("application/json")]
        //[Route("RevokeToken")]
        //public async Task<IActionResult> Revoke()
        //{
        //    var userName = User.Claims.ToList()[0].Value;

        //    var user = _userService.GetUserByUserName(userName).Result;
        //    if (user == null) return BadRequest();

        //    user.RefreshToken = null;
        //    var result = _userService.EditUserRefreshToken(userName, null);
        //    return NoContent();
        //}

        private UserBO AuthenticateUser(UserBO login)
        {
            UserBO user          = null;
            UserBO userAdmin     = null;
            UserBO userKhachHang = null;

            user = _userService.GetUserByTenDangNhap(login.TenTaiKhoan).Result;

            if (user != null)
            {
                bool validPassword = BCryptService.CheckPassword(login.MatKhau, user.MatKhau);

                if (validPassword)
                {
                    if (user.IdLoaiTaiKhoan == 1)
                    {
                        userKhachHang = _userService.GetThongTinTaiKhoanKhachHang(user.MaTk).Result;
                        return(userKhachHang);
                    }
                    else
                    {
                        if (user.IdLoaiTaiKhoan == 4)
                        {
                            return(user);
                        }
                        else
                        {
                            userAdmin = _userService.GetThongTinTaiKhoanAdmin(user.MaTk).Result;
                            return(userAdmin);
                        }
                    }
                }
            }

            return(null);
        }
Esempio n. 7
0
        public async Task <ActionResult <dynamic> > UpdateUser([FromBody] User user)
        {
            List <string> validations_erro = new List <string>();
            bool          updadtePassword  = false;

            if (string.IsNullOrEmpty(user.Name))
            {
                validations_erro.Add("User name is required");
            }

            if (string.IsNullOrEmpty(user.CPF))
            {
                validations_erro.Add("User CPF is required");
            }

            if (user.Id <= 0)
            {
                validations_erro.Add("User id is required");
            }

            if (string.IsNullOrEmpty(user.Email))
            {
                validations_erro.Add("User e-mail is required");
            }

            if (user.Role <= 0)
            {
                validations_erro.Add("User role is required");
            }

            if (!string.IsNullOrEmpty(user.Password))
            {
                if (!(!string.IsNullOrEmpty(user.Login) && !string.IsNullOrEmpty(user.Password)))
                {
                    validations_erro.Add("User login and password is required");
                }
                else
                {
                    updadtePassword = true;
                    user.Password   = BCryptService.GenerateBCryptHash(user.Password, workfactor);
                }
            }

            try
            {
                var responseCPFValid = HttpService.Get(urlValidateCPF + "/" + user.CPF);
                if (responseCPFValid.httpStatusCode != 200 || responseCPFValid.message != "Autorizado")
                {
                    validations_erro.Add("CPF não é valido");
                }

                var older_user_cpf = userRepository.GetUser(user.CPF);
                if (older_user_cpf != null && older_user_cpf.Id != user.Id)
                {
                    validations_erro.Add("Already exists user with this CPF");
                }

                var older_user_login = userRepository.GetUserLogin(user.Login);
                if (older_user_login != null && older_user_cpf.Id != user.Id)
                {
                    validations_erro.Add("Already exists user with this Login");
                }
            }
            catch (Exception ex1)
            {
                return(CatchError(ex1, string.Format("Update user id({0}), validate CPF", user.Id)));
            }

            if (validations_erro.Count() > 0)
            {
                return(BadRequest(new { success = false, data = new { }, messages = validations_erro }));
            }

            try
            {
                user.Name  = user.Name.ToUpper();
                user.Login = user.Login.ToLower();
                user.Email = user.Email.ToLower();
                User data_user = userRepository.Save(user, updadtePassword);
                return(Ok(new { success = true, data = data_user, messages = "Item successfull updated" }));
            }
            catch (Exception ex2)
            {
                return(CatchError(ex2, string.Format("Update user id({0})", user.Id)));
            }
        }
Esempio n. 8
0
 private bool CheckHash(string textValue, string hashValue)
 {
     return(BCryptService.CheckPassword(textValue, hashValue));
 }
Esempio n. 9
0
        public ReturnCertEntity PostLoginData(LoginEntity loginData)
        {
            var result = BCryptService.Verify(loginData, _appSettings);

            return((result.err == true) ? new ReturnCertEntity() : result.data);
        }
Esempio n. 10
0
        public ReturnCertEntity PostRegistData(RegistEntity registData)
        {
            var result = BCryptService.Regist(registData, _appSettings);

            return((result.err == true)? new ReturnCertEntity() : result.data);
        }