public ActionResult Register(RegistViewModel model) { if (ModelState.IsValid) { User user = null; using (BotAppContext db = new BotAppContext()) { user = db.Users.FirstOrDefault(u => u.Login == model.Login); } if (user == null) { // создаем нового пользователя using (BotAppContext db = new BotAppContext()) { db.Users.Add(new User { Login = model.Login, Password = model.Password, Name = model.UserName, RoleId = 2 }); db.SaveChanges(); user = db.Users.Where(u => u.Login == model.Login && u.Password == model.Password).FirstOrDefault(); } // если пользовател успешно добавлен в БД if (user != null) { FormsAuthentication.SetAuthCookie(model.Login, true); return(RedirectToAction("AddToken", "LongPollServer")); } } else { ModelState.AddModelError("", "Пользователь с таким логином уже существует"); } } return(View(model)); }
public ActionResult AddCmd(Command command) { User user = _db.Users.Where(u => u.Login == HttpContext.User.Identity.Name).FirstOrDefault(); if (user == null) { return(RedirectToAction("LogOut", "Account")); } if (ModelState.IsValid) { command.UserId = user.Id; _db.Commands.Add(command); _db.SaveChanges(); return(RedirectToAction("Index")); } return(View(command)); }