public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { // Attempt to add the user try { db.Users.Add(new User() { Email = model.Email, Password = PasswordHash.ComputeHash(model.Password), Login = model.UserName, Role = 1 }); db.SaveChanges(); FormsAuthentication.SetAuthCookie(model.UserName, createPersistentCookie: false); return RedirectToAction("Index", "Home"); } catch (Exception e) { ModelState.AddModelError("", e.ToString()); } } // If we got this far, something failed, redisplay form return View(model); }
public ActionResult JsonRegister(RegisterModel model) { if (ModelState.IsValid) { // Attempt to add the user try { db.Users.Add(new User() { Email = model.Email, Password = PasswordHash.ComputeHash(model.Password), Login = model.UserName, Role = 2 }); db.SaveChanges(); FormsAuthentication.SetAuthCookie(model.UserName, createPersistentCookie: false); return Json(new { success = true }); } catch (Exception e) { ModelState.AddModelError("", e.ToString()); } } // If we got this far, something failed return Json(new { errors = GetErrorsFromModelState() }); }