public IActionResult ResetPassword(User user) { ViewBag.login = true; ModelState.Clear(); var allUsers = _userDataAccess.GetAllUsers(); if (ModelState.IsValid) { if (!allUsers.Any(x => x.Email == user.Email)) { ModelState.AddModelError(string.Empty, "User with this email doesn't exists!"); return(View()); } else { string saltBytes = HashPassword.GenerateSalt(); var userId = _userDataAccess.GetUserId(user); var userWithNewPass = _userDataAccess.GetUserById(userId); userWithNewPass.Password = HashPassword.HashPasswordFunction(user.Password, saltBytes); userWithNewPass.Salt = saltBytes; userWithNewPass.ConfirmAccount = true; _userDataAccess.UpdateUser(userWithNewPass); ModelState.AddModelError(string.Empty, "You have successfuly changed your password, please log in!"); return(View("Views/Account/RegisterLogin.cshtml")); } } ModelState.AddModelError(string.Empty, "Please provide the necessary data!"); return(View()); }
public IActionResult Register(User user) { ModelState.Clear(); if (ModelState.IsValid) { ViewBag.register = true; Studio newStudio = new Studio(); string saltBytes = HashPassword.GenerateSalt(); user.Password = HashPassword.HashPasswordFunction(user.Password, saltBytes); user.Salt = saltBytes; newStudio.Name = user.StudioName; var allUsers = _userDataAccess.GetAllUsers(); var allStudios = _studioDataAccess.GetAllStudios(); if (allUsers.Any(x => x.Username == user.Username)) { ModelState.AddModelError(string.Empty, "User with this username already exists"); } if (allUsers.Any(x => x.Email == user.Email)) { ModelState.AddModelError(string.Empty, "User with this email already exists"); } if (allStudios.Any(x => x.Name == user.StudioName)) { ModelState.AddModelError(string.Empty, "This studio name already exists"); } else if (ModelState.IsValid) { try { using (TransactionScope scope = new TransactionScope()) { _studioDataAccess.AddNewStudio(newStudio); int studioId = _studioDataAccess.GetStudioId(newStudio); _userDataAccess.AddNewUser(user, studioId); int userId = _userDataAccess.GetUserId(user); var path = Url.Action("AuthenticateLogin", "Home", new { userId = user.Id }, protocol: HttpContext.Request.Scheme); string message = "Please confirm your account by clicking <a href=\"" + path + "\">here</a>"; _email.SendGrid(user.Email, message); scope.Complete(); return(View("Views/Account/ConfirmEmail.cshtml")); } } catch (ThreadAbortException ex) { ModelState.AddModelError(ex.Message, ""); } } } return(View("Views/Account/RegisterLogin.cshtml")); }