public ActionResult Login(string returnUrl, User model) { if (ModelState.IsValid) { try { var hashPass = DataProvider.getHashPass(model.Password); var user = DataProvider.Instance.DataBase.Users.Where(x => x.UserName == model.UserName && x.Password == hashPass).SingleOrDefault(); if (user != null) { Session["UserID"] = user.Id; Session["UserName"] = user.UserName; Session["UserAuthority"] = user.UserAuthority.DisplayName; return(Redirect(returnUrl)); } else { ModelState.AddModelError("", "Invalid username or password."); } } catch { ModelState.AddModelError("", "Invalid username or password."); } } // If we got this far, something failed, redisplay form ViewBag.returnUrl = returnUrl; return(View()); }
public ActionResult Manage(ManageModel model) { if (ModelState.IsValid) { try { DateTime?birthday = DateTime.Parse(model.Birthday); string hashPass = DataProvider.getHashPass(model.Password); string username = Session["UserName"].ToString(); User user = DataProvider.Instance.DataBase.Users.Where(x => x.UserName == username && x.Password == hashPass).SingleOrDefault(); if (user != null) { user.FullName = model.FullName; user.Address = model.Address; user.Email = model.Email; user.Phone = model.Phone; user.Birthday = birthday; DataProvider.Instance.DataBase.SaveChanges(); ModelState.AddModelError("", "Your information was updated!"); } else { ModelState["Password"].Errors.Add("Your password is incorrect! Please try again"); } } catch { ModelState.AddModelError("", "Your birthday format is invalid! Please try again"); } } // If we got this far, something failed, redisplay form return(View(model)); }
public ActionResult ChangePassword(ChangePasswordModel model) { if (ModelState.IsValid) { string hashPass = DataProvider.getHashPass(model.Password); string hashNewPass = DataProvider.getHashPass(model.NewPassword); string username = Session["UserName"].ToString(); User user = DataProvider.Instance.DataBase.Users.Where(x => x.UserName == username && x.Password == hashPass).SingleOrDefault(); if (user != null) { user.Password = hashNewPass; DataProvider.Instance.DataBase.SaveChanges(); ModelState.AddModelError("", "Your password was changed!"); } else { ModelState["Password"].Errors.Add("Your password is incorrect! Please try again"); } } // If we got this far, something failed, redisplay form return(View()); }
public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { if (DataProvider.Instance.DataBase.Users.Where(x => x.UserName == model.UserName).SingleOrDefault() != null) { ModelState["UserName"].Errors.Add("The UserName is existed. Please enter other UserName and try again!"); } else { string hashPass = DataProvider.getHashPass(model.Password); User user = new User() { Id = Guid.NewGuid().ToString(), UserName = model.UserName, Password = hashPass, FullName = model.FullName, Address = model.Address, Email = model.Email, Phone = model.Phone, Birthday = model.Birthday, Point = 0, IdUserAuthority = DataProvider.Instance.DataBase.UserAuthorities.Where(x => x.DisplayName == "Customer").SingleOrDefault().Id, }; DataProvider.Instance.DataBase.Users.Add(user); DataProvider.Instance.DataBase.SaveChanges(); Session["UserID"] = user.Id; Session["UserName"] = user.UserName; Session["UserAuthority"] = user.UserAuthority.DisplayName; return(RedirectToAction("Index", "Home")); } } // If we got this far, something failed, redisplay form return(View()); }
public ActionResult Buy(BuyModel model) { // If haven't login => login and come back if (Session["UserID"] == null) { return(RedirectToAction("Login", "Users", new { @returnUrl = Request.Url.OriginalString })); } if (ModelState.IsValid) { string userid = Session["UserID"].ToString(); string hashPass = DataProvider.getHashPass(model.Password); User user = DataProvider.Instance.DataBase.Users.Where(x => x.Id == userid && x.Password == hashPass).SingleOrDefault(); if (user != null) { // Add new output var output = DataProvider.Instance.DataBase.Outputs.Where(x => x.IdCustomer == userid).SingleOrDefault(); if (output == null) { output = new Output() { Id = Guid.NewGuid().ToString(), IdCustomer = userid }; DataProvider.Instance.DataBase.Outputs.Add(output); DataProvider.Instance.DataBase.SaveChanges(); } var product = DataProvider.Instance.DataBase.Products.Where(x => x.Id == model.ProductID).SingleOrDefault(); OutputInfo outputinfo = new OutputInfo() { Id = Guid.NewGuid().ToString(), IdProduct = model.ProductID, IdOutput = output.Id, OutputPrice = product.Price.Value * (100 - product.Discount.Value) / 100, Count = model.Count, DateOutput = DateTime.Now, IsCheckOut = 0, }; DataProvider.Instance.DataBase.OutputInfoes.Add(outputinfo); // remove from basket if (model.FromBasket) { var basket = DataProvider.Instance.DataBase.Baskets.Where(x => x.IdCustomer == userid).SingleOrDefault(); if (basket != null) { var bi = DataProvider.Instance.DataBase.BasketInfoes.Where(x => x.IdProduct == model.ProductID && x.IdBasket == basket.Id).SingleOrDefault(); if (bi != null) { DataProvider.Instance.DataBase.BasketInfoes.Remove(bi); if (basket.BasketInfoes.Count == 0) { DataProvider.Instance.DataBase.Baskets.Remove(basket); } } } } DataProvider.Instance.DataBase.SaveChanges(); ViewBag.Bought = true; return(View(model)); } else { ModelState.AddModelError("", "Your password is incorrect!"); } } ViewBag.Bought = null; return(View(model)); }