public ActionResult Register(User user) { try { if (ModelState.IsValid) { using (OpsIntel1Entities1 context = new OpsIntel1Entities1()) { User newUser = new User(); // var newUser = context.Users.Create(); newUser.Email = user.Email; newUser.Password = user.Password; newUser.Username = user.Username; newUser.FirstName = user.FirstName; newUser.LastName = user.LastName; newUser.CreatedDate = DateTime.Now; context.Users.Add(newUser); context.SaveChanges(); return RedirectToAction("LogIn", "Home"); } } else { ModelState.AddModelError("", "Data is not correct"); } } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } return View(); }
public ActionResult LogIn(User user) { //if (ModelState.IsValid) //{ if (IsValid(user.Username, user.Password)) { TempData["Username"] = user.Username; //FormsAuthentication.SetAuthCookie(user.Username, false); return RedirectToAction("Index", "Home"); } else { ModelState.AddModelError("", "Login details are wrong."); } return View(user); }