public ActionResult EditUser(int itemId, string act) { switch (act) { case "edit": var user = db.Users.Find(itemId); if (user == null) { return(RedirectToAction("User")); } var model = new UserViewModel() { Uname = user.Uname, Upass = MyHelpers.Md5(user.Upass) }; return(View(model)); case "delete": var users = db.Users.Find(itemId); db.Users.Remove(users); db.SaveChanges(); return(RedirectToAction("User")); default: Response.StatusCode = 404; return(HttpNotFound()); } }
public ActionResult Login(LoginViewModel model) { if (ModelState.IsValid) { var pass = MyHelpers.Md5(model.Upass); var admin = db.Users.SingleOrDefault(e => e.Uname == model.Uname && e.Upass == pass); if (admin != null) { try { FormsAuthentication.SetAuthCookie(model.Uname, false); var authTicket = new FormsAuthenticationTicket(1, admin.Uname, DateTime.Now, DateTime.Now.AddDays(1), false, admin.Roles); string encryptedTicket = FormsAuthentication.Encrypt(authTicket); var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket); HttpContext.Response.Cookies.Add(authCookie); return(RedirectToAction("Index", "Manage")); } catch (Exception e) { ModelState.AddModelError("", e.Message); return(View(model)); } } ModelState.AddModelError("", "Đăng nhập thất bại!"); } return(View(model)); }
public ActionResult RegisterUser(LoginViewModel model) { var pass = MyHelpers.Md5(model.Upass); var admin = db.Users.SingleOrDefault(e => e.Uname == model.Uname); if (ModelState.IsValid) { try { if (admin == null) { var user = new DAN.Models.User() { Uname = model.Uname, Upass = MyHelpers.Md5(model.Upass), Roles = "0" }; db.Users.Add(user); db.SaveChanges(); ModelState.AddModelError("", "Đăng ký thành công!"); return(View("Login")); } else { ModelState.AddModelError("", "Đăng ký thất bại!"); return(View("Register")); } }catch (Exception e) { ModelState.AddModelError("", "Đăng ký thất bại!"); return(View("Register")); } } return(View("Index")); }
public ActionResult EditUser(UserViewModel model) { var user = db.Users.Find(model.Uid); user.Uname = model.Uname; user.Upass = MyHelpers.Md5(user.Upass); db.SaveChanges(); return(RedirectToAction("User")); }
public ActionResult AddUser(UserViewModel model) { if (ModelState.IsValid) { var user = new DAN.Models.User() { Uname = model.Uname, Upass = MyHelpers.Md5(model.Upass) }; db.Users.Add(user); db.SaveChanges(); return(RedirectToAction("User")); } return(View(model)); }