public void CreateAccount(RegisterModel model) { using (var context = new CPS_SolutionEntities()) { // Duplicate account var account = context.Accounts.FirstOrDefault(x => x.Username == model.UserName); if (account != null) { throw new MembershipCreateUserException(MembershipCreateStatus.DuplicateUserName); } // Duplicate email var email = context.Accounts.FirstOrDefault(x => x.Email == model.Email); if(email !=null) { throw new MembershipCreateUserException(MembershipCreateStatus.DuplicateEmail); } var newUser = new Account { Username = model.UserName, Password = model.Password, RoleID = model.RoleId, Email = model.Email, IsActive = true }; context.Accounts.Add(newUser); context.SaveChanges(); } }
public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { // Attempt to register the user try { // Register member model.RoleId = (int)SystemRole.Member; var accountHelper = new AccountHelper(); accountHelper.CreateAccount(model); WebSecurity.Login(model.UserName, model.Password); Session["Username"] = model.UserName; return RedirectToAction("Index", "HighLight"); } catch (MembershipCreateUserException e) { ModelState.AddModelError("", ErrorCodeToString(e.StatusCode)); } } // If we got this far, something failed, redisplay form return View(model); }