public ActionResult Register(RegisterDTO oDTO) { if (!validateRegistration(oDTO)) return View(oDTO); if (DBHandler.registerUser(oDTO)) return RedirectToAction("Login"); else { // If we got this far, something failed, redisplay form ModelState.AddModelError("", "The user name is already taken."); return View(oDTO); } }
private bool validateRegistration(RegisterDTO oDTO) { if (string.IsNullOrEmpty(oDTO.UserName) || string.IsNullOrEmpty(oDTO.Password) || string.IsNullOrEmpty(oDTO.ConfirmPassword) ){ // If we got this far, something failed, redisplay form ModelState.AddModelError("", "Please fix the error"); return false; } if (oDTO.Password != oDTO.ConfirmPassword) { ModelState.AddModelError("", "Please fix the error"); return false; } return true; }
public static bool registerUser(RegisterDTO oDTO) { if(!isUserNameUnique(oDTO.UserName)) return false; using (var db = new OrganizerDBContext()) { AppUser oAppUser = new AppUser(); oAppUser.UserName = oDTO.UserName; oAppUser.Password = PasswordHash.CreateHash(oDTO.Password); db.AppUsers.Add(oAppUser); db.Entry(oAppUser).State = System.Data.EntityState.Added; db.SaveChanges(); } return true; }