Example #1
0
 public ActionResult Login(User ac)
 {
     ac.Password = CommonConstant.HashPassword(ac.Password);
     using (BSDBContext db = new BSDBContext())
     {
         var result = db.Users.Where(p => p.Username == ac.Username && p.Password == ac.Password).Count();
         if (result == 0)
         {
             ModelState.AddModelError("", "Tài khoản không tồn tại hoặc Tên đăng nhập, mật khấu bị sai.");
         }
         else
         {
             var userID  = db.Users.Where(a => a.Username == ac.Username).FirstOrDefault().UserID;
             var name    = db.Users.Where(a => a.Username == ac.Username && a.Password == ac.Password).FirstOrDefault().Username;
             var blockID = db.Users.Where(a => a.Username == ac.Username).FirstOrDefault().isActivated;
             var roleID  = db.Users.Where(a => a.Username == ac.Username).FirstOrDefault().UserRoleID;
             Session["userHello"] = name;
             Session["userName"]  = ac.Username;
             Session["UserID"]    = userID;
             Session["blockID"]   = blockID;
             Session["roleID"]    = roleID;
             return(Redirect(Request.Url != null ? Request.Url.ToString() : Url.Action("Index")));
         }
     }
     return(View());
 }
Example #2
0
 public ActionResult Register(User model)
 {
     if (ModelState.IsValid)
     {
         using (BSDBContext db = new BSDBContext())
         {
             var user = db.Users.FirstOrDefault(p => p.Username == model.Username);
             if (user == null)
             {
                 model.UserRoleID  = 1;
                 model.isActivated = true;
                 model.Password    = CommonConstant.HashPassword(model.Password);
                 db.Users.Add(model);
                 db.SaveChanges();
             }
             else
             {
                 ViewBag.Message = "Tài khoản " + model.Username + " đã tồn tại";
                 return(View());
             }
         }
         ModelState.Clear();
         ViewBag.Message = "Đăng ký thành công cho tài khoản " + model.Username;
     }
     return(RedirectToAction("Login"));
 }
Example #3
0
 public ActionResult Register(User account)
 {
     if (account.Username == null || account.Password == null)
     {
         return(View());
     }
     if (ModelState.IsValid)
     {
         using (BSDBContext db = new BSDBContext())
         {
             var user = db.Users.FirstOrDefault(p => p.Username == account.Username);
             if (user == null)
             {
                 account.UserRoleID  = 2;
                 account.isActivated = true;
                 account.Password    = CommonConstant.HashPassword(account.Password);
                 account.Birthday    = DateTime.Now;
                 db.Users.Add(account);
                 db.SaveChanges();
             }
             else
             {
                 ViewBag.Message = "UserName already exists" + account.Username;
                 return(View());
             }
         }
         ModelState.Clear();
         ViewBag.Message = "Successfully Registered Mr. " + account.Username;
     }
     return(RedirectToAction("Index"));
 }
Example #4
0
        public JsonResult ExecuteLogin(User model)
        {
            model.Password = CommonConstant.HashPassword(model.Password);
            bool check = new AccountDAO().checkAccount(model.Username, model.Password);

            if (check)
            {
                var roleID = db.Users.Where(a => a.Username == model.Username).FirstOrDefault().UserRoleID;
                Session["roleID"] = roleID;
                if (check && ((int)Session["roleID"] == 1))
                {
                    Session["UserName"] = model.Username;
                    return(Json(new { status = true }));
                }
                else
                {
                    return(Json(new { status = false }));
                }
            }
            return(Json(new { status = false }));
        }