public ActionResult Login(LoginModel model) { ShoppingStoreContext db = new ShoppingStoreContext(); if (ModelState.IsValid) { // поиск пользователя в бд User user = null; user = db.Users.FirstOrDefault(u => u.UserEmail == model.Email && u.Password == model.Password); if (user != null) { FormsAuthentication.SetAuthCookie(model.Email, true); //ViewBag.userid = user.UserId; MigrateShoppingCart(model.Email); return(RedirectToAction("Index", "Store")); } else { ModelState.AddModelError("", "Пользователя с таким логином и паролем нет"); } } return(View(model)); }
public ActionResult MyOffice([Bind(Include = "UserId,UserAge,UserName,UserEmail,UserMobileNumber,Password")] User user) { if (ModelState.IsValid) { using (ShoppingStoreContext db = new ShoppingStoreContext()) { if (user.UserEmail != User.Identity.Name) { User userCheck = null; userCheck = db.Users.FirstOrDefault(u => u.UserEmail == user.UserEmail); if (userCheck != null) { ModelState.AddModelError("", "Пользователь с таким логином уже существует"); return(View(user)); } } db.Entry(user).State = EntityState.Modified; db.SaveChanges(); if (user != null) { FormsAuthentication.SetAuthCookie(user.UserEmail, true); } return(RedirectToAction("MyOffice")); } } return(View(user)); }
public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { User user = null; using (ShoppingStoreContext db = new ShoppingStoreContext()) { user = db.Users.FirstOrDefault(u => u.UserEmail == model.UserEmail); } if (user == null) { // создаем нового пользователя using (ShoppingStoreContext db = new ShoppingStoreContext()) { db.Users.Add(new User { UserEmail = model.UserEmail, Password = model.Password, UserAge = model.UserAge, UserName = model.UserName, UserMobileNumber = model.UserMobileNumber }); db.SaveChanges(); user = db.Users.Where(u => u.UserEmail == model.UserEmail && u.Password == model.Password).FirstOrDefault(); } // если пользователь удачно добавлен в бд if (user != null) { FormsAuthentication.SetAuthCookie(model.UserEmail, true); MigrateShoppingCart(model.UserEmail); return(RedirectToAction("Index", "Store")); } } else { ModelState.AddModelError("", "Пользователь с таким логином уже существует"); } } return(View(model)); }