private UserInfo Authenticate(LoginView login, out User user, out Role role)
        {
            UserModels sv  = new UserModels();
            UserInfo   iit = new UserInfo();

            user = sv.GetUserbyUserName(login.UserName);
            if (user != null && MD5Extend.EncodePassword(login.Password) == user.Password && user.Online == true)
            {
                role = sv.GetRolebyId(user.Role);
                iit  = sv.GetUserInforByEmail(user.Username);
                if (iit != null)
                {
                    return(iit);
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                role = null;
                return(null);
            }
        }
        public IActionResult Refresh([FromBody] RefreshTokenResource resource)
        {
            UserModels    sv       = new UserModels();
            IActionResult response = null;

            var identity = (ClaimsIdentity)User.Identity;
            IEnumerable <Claim> claims = identity.Claims;
            var userLogin = claims.FirstOrDefault(c => c.Type == ClaimTypes.Email).Value;

            User user = sv.GetUserbyUserName(userLogin);

            if (user != null && user.Token == resource.Token)
            {
                var role        = sv.GetRolebyId(user.Role);
                var expiresDate = DateTime.Now.AddHours(12);
                var now         = DateTime.Now;
                var userInfo    = sv.GetUserInforByEmail(user.Username);

                if (!string.IsNullOrEmpty(user.Token) && user.Expire != null && user.Expire.Value > DateTime.Now)
                {
                    var tokenResource = new TokenResource
                    {
                        Token  = user.Token,
                        Expiry = user.Expire.Value
                    };
                    response = Json(tokenResource);
                }
                else
                {
                    var tokenResource = BuildToken(userInfo, user, role, now, expiresDate);
                    response = Json(tokenResource);
                }
            }

            return(response);
        }
        public IActionResult ValidateUser([FromBody] AdminUserValidateView userView)
        {
            IActionResult response   = null;
            UserModels    userModels = new UserModels();
            User          user       = new User();
            var           mess       = string.Empty;
            string        rt         = string.Empty;
            bool          is_valid   = true;

            if (userView.IsCreate)
            {
                if (!string.IsNullOrEmpty(userView.Username))
                {
                    user = userModels.GetUserbyUserName(userView.Username);

                    if (user != null)
                    {
                        is_valid = false;
                        if (mess == string.Empty)
                        {
                            response = Json(new { code = Constant.Duplicate, message = Constant.MessageDuplicate, field = "username" });
                        }
                    }
                }
            }

            ////validation server
            if (string.IsNullOrEmpty(userView.Username))
            {
                is_valid = false;
                if (mess == string.Empty)
                {
                    mess     = Constant.MessageDataEmpty;
                    response = Json(new { code = Constant.Empty, message = mess, field = "username" });
                }
            }

            ////UserName duplicate Admin
            if (userView.Username == "Admin")
            {
                is_valid = false;
                if (mess == string.Empty)
                {
                    mess     = Constant.MessageNotValid;
                    response = Json(new { code = Constant.Fail, message = mess, field = "username" });
                }
            }

            if (userView.IsCreate)
            {
                // validation password
                if (string.IsNullOrEmpty(userView.Password))
                {
                    is_valid = false;
                    if (mess == string.Empty)
                    {
                        mess     = Constant.MessageDataEmpty;
                        response = Json(new { code = Constant.Empty, message = mess, field = "password" });
                    }
                }

                if (string.IsNullOrEmpty(userView.ConfirmPassword))
                {
                    is_valid = false;
                    if (mess == string.Empty)
                    {
                        mess     = Constant.MessageDataEmpty;
                        response = Json(new { code = Constant.Empty, message = mess, field = "confirmPassword" });
                    }
                }

                if (userView.Password != userView.ConfirmPassword)
                {
                    is_valid = false;
                    if (mess == string.Empty)
                    {
                        mess     = Constant.MessageConfirmPassword;
                        response = Json(new { code = Constant.Fail, message = mess, field = "confirmPassword" });
                    }
                }
            }

            if (userModels.GetRolebyId(userView.Role) == null)
            {
                is_valid = false;
                if (mess == string.Empty)
                {
                    mess     = Constant.MessageNotExist;
                    response = Json(new { code = Constant.NotExist, message = mess, field = "role" });
                }
            }

            if (is_valid)
            {
                response = Json(new { code = Constant.Success, message = Constant.MessageOk });
            }

            return(response);
        }