public ActionResult ChangePassword(UserViewModel user) { bool changePasswordSucceeded; try { if (user.ConfirmNewPassword == user.NewPassword) { string cryptedPassword = encryptPassword(user.OldPassword, user.Username); int userId = ResponseReader.convertTo<int>(emAPI.validateUser(user.Username, cryptedPassword)); if (userId == int.Parse(User.Identity.Name)) { cryptedPassword = encryptPassword(user.NewPassword, user.Username); changePasswordSucceeded = ResponseReader.convertTo<bool>(emAPI.updatePassword(userId, cryptedPassword)); } else { changePasswordSucceeded = false; ModelState.AddModelError("", "There has been an error, please try again"); } } else { changePasswordSucceeded = false; ModelState.AddModelError("", "New password does not match new password confirmation"); } } catch (Exception) { changePasswordSucceeded = false; } if (changePasswordSucceeded) { return RedirectToAction("ChangePasswordSuccess"); } else { ModelState.AddModelError("", "The current password is incorrect or the new password is invalid."); } // If we got this far, something failed, redisplay form return View(user); }
public ActionResult Register(UserViewModel model) { ///check if username & email are unique /// bool userNameIsUnique = false; bool emailIsUnique = false; userNameIsUnique = ResponseReader.convertTo<bool>(emAPI.usernameIsUnique(model.User.Username)); emailIsUnique = ResponseReader.convertTo<bool>(emAPI.emailIsUnique(model.User.Email)); if (userNameIsUnique) { if (emailIsUnique) { if (model.ConfirmNewPassword == model.NewPassword && model.NewPassword != null) { ///ok to create the user ///hash the password string cryptedPassword = encryptPassword(model.NewPassword, model.Username); int newUserId = 0; try { FormsAuthentication.SignOut(); ///get user id from emAPI newUserId = ResponseReader.convertTo<int>(emAPI.createUser(model.User.Username, model.User.Forename, model.User.Surname, cryptedPassword, model.User.Email)); if (newUserId != 0) { ///login FormsAuthentication.SetAuthCookie(newUserId.ToString(), false); } else { throw new Exception(); } } catch { ModelState.AddModelError("", "There has been an error registering your details. Please try again."); return View(model); } if (model.CreateAPropertyNow) { ///redirect to create a property return RedirectToAction("Create", "Property", new { userId = newUserId }); } else { ///redirect to home return RedirectToAction("UserHome", "Home", new { id = newUserId }); } } else { ///the passwords do not match ModelState.AddModelError("", "Confirmation password does not match"); } } else { ///the email is not unique ModelState.AddModelError("", ErrorCodeToString(MembershipCreateStatus.DuplicateEmail)); } } else { ///the username is not unique ModelState.AddModelError("", ErrorCodeToString(MembershipCreateStatus.DuplicateUserName)); } return View(model); }