Ejemplo n.º 1
0
        public ActionResult Create([Bind(Include = "ID,UserName,PassWord,Name,Address,Email,Phone,Image,CreatedDate,CreatedBy,ModifiedDate,ModifiedBy,Status")] User user)
        {
            Boolean check = false;
            var     model = new UserDAO();

            foreach (var item in model.UsLogin())
            {
                if (user.UserName == item.UserName)
                {
                    check = true;
                    break;
                }
            }
            if (ModelState.IsValid)
            {
                if (check)
                {
                    ModelState.AddModelError("", "UserName da ton tai");
                }
                else
                {
                    SetAlert("Them User thanh cong", "success");
                    user.PassWord = Encriptor.MD5Hash(user.PassWord);

                    db.Users.Add(user);
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
            }
            return(View(user));
        }
Ejemplo n.º 2
0
        public ActionResult Index(LoginModel model)
        {
            var dao = new UserDao();
            var res = dao.Login(model.UserName, Encriptor.MD5Hash(model.Password));

            if (ModelState.IsValid)
            {
                switch (res)
                {
                case 0:
                    ModelState.AddModelError("", CommonConstant.LOGIN_FAIL);
                    break;

                case -1:
                    ModelState.AddModelError("", CommonConstant.USER_IS_BLOCKED);
                    break;

                default:
                    var user        = dao.GetNguoiDungByName(model.UserName);
                    var userSession = new UserSession();
                    userSession.UserName = user.UserName;
                    userSession.UserID   = user.ID;
                    userSession.UserRole = user.Role;
                    Session.Add(CommonConstant.USER_SESSION, userSession);
                    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                    return(RedirectToAction("Dashboard", "AdminHome"));
                }
            }
            return(View(model));
        }
Ejemplo n.º 3
0
 public ActionResult Edit([Bind(Include = "ID,UserName,PassWord,Name,Address,Email,Phone,Image,CreatedDate,CreatedBy,ModifiedDate,ModifiedBy,Status")] User user)
 {
     if (ModelState.IsValid)
     {
         SetAlert("Sua thanh cong", "success");
         user.PassWord        = Encriptor.MD5Hash(user.PassWord);
         db.Entry(user).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(user));
 }
Ejemplo n.º 4
0
 public ActionResult Login(LoginModel model)
 {
     if (ModelState.IsValid)
     {
         var dao = new UserDAO();
         var res = dao.Login(model.UserName, Encriptor.MD5Hash(model.Password));
         if (res == 1)
         {
             var user        = dao.GetByID(model.UserName);
             var UserSession = new UserLogin();
             UserSession.UserName = user.UserName;
             UserSession.UserID   = user.ID;
             Session.Add(CommonConstants.User_Session, UserSession);
             Session["UserID"] = user.ID;
             return(RedirectToAction("Index", "Home"));
         }
         else
         {
             if (res == -1)
             {
                 ViewBag.error = "Mật khẩu nhập vào không đúng";
                 return(View("Index"));
             }
             else
             {
                 if (res == 0)
                 {
                     ViewBag.error = "Tài khoản không tồn tại";
                     return(View("Index"));
                 }
                 else
                 {
                     ViewBag.error = "Tài khoản bị khóa";
                     return(View("Index"));
                 }
             }
         }
     }
     else
     {
         ViewBag.error = "Login thất bại";
         return(View("Index"));
     }
 }