public async Task <IActionResult> ConfirmEmail(string email, string emailConfirmationToken) { if (string.IsNullOrWhiteSpace(email) || string.IsNullOrWhiteSpace(emailConfirmationToken)) { return(base.RedirectToIndexActionInHomeController()); } User userDb = await this.usersService.FindByEmailAsync(email); if (userDb == null) { TempData["ErrorMessage"] = "A user with this email doesn't exist!"; return(View("ValidationErrorsWithoutSpecificModel")); } UsersServiceResultDTO confirmEmailDTO = await this.usersService.ConfirmEmailAsync(userDb, emailConfirmationToken); if (!confirmEmailDTO.IsSucceed) { base.AddValidationErrorsToModelState(confirmEmailDTO.ErrorMessages); return(View("ValidationErrorsWithoutSpecificModel")); } TempData["SuccessMessage"] = "Email confirmed successfully!"; return(base.RedirectToIndexActionInHomeController()); }
public async Task <IActionResult> ChangePasswordAsync(ChangePasswordViewModel changePasswordViewModel) { if (!ModelState.IsValid) { return(View(changePasswordViewModel)); } User userDb = await usersService.GetAll(x => x.Id == changePasswordViewModel.UserId) .FirstOrDefaultAsync(); if (userDb == null) { TempData["ErrorMessage"] = "A user with this id doesn't exist!"; return(RedirectToIndexActionInHomeController()); } string passwordResetToken = await usersService.GeneratePasswordResetTokenAsync(userDb); UsersServiceResultDTO changePasswordResultDTO = await usersService.ResetPasswordAsync(userDb, passwordResetToken, changePasswordViewModel.Password); if (!changePasswordResultDTO.IsSucceed) { base.AddValidationErrorsToModelState(changePasswordResultDTO.ErrorMessages); return(View(changePasswordViewModel)); } TempData["SuccessMessage"] = "Password changed successfully!"; return(RedirectToListAllActionInCurrentController()); }
private UsersServiceResultDTO GetUsersServiceResultDTO(IdentityResult identityResult) { UsersServiceResultDTO registerResult = new UsersServiceResultDTO(); if (identityResult.Succeeded) { registerResult.IsSucceed = true; return(registerResult); } registerResult.ErrorMessages = this.GetErrorMessages(identityResult); return(registerResult); }
public async Task <UsersServiceResultDTO> CreateAsync(User userDb, string password) { IdentityResult identityResult = await userManager.CreateAsync(userDb, password); UsersServiceResultDTO registerResult = new UsersServiceResultDTO(); if (identityResult.Succeeded) { registerResult.IsSucceed = true; return(registerResult); } registerResult.ErrorMessages = this.GetErrorMessages(identityResult); return(registerResult); }
public async Task <IActionResult> RegisterAsync(RegisterReporterViewModel registerReporterViewModel) { User userDb = new User { UserName = registerReporterViewModel.Username, Email = registerReporterViewModel.Email }; UsersServiceResultDTO registerResultDTO = await this.usersService.CreateAsync(userDb); if (!registerResultDTO.IsSucceed) { base.AddValidationErrorsToModelState(registerResultDTO.ErrorMessages); return(View(registerReporterViewModel)); } string passwordResetToken = await usersService.GeneratePasswordResetTokenAsync(userDb); string setPasswordAbsoluteUrl = Url.Action(nameof(UsersController.SetPassword), this.GetControllerName(nameof(UsersController)), new { userId = userDb.Id, passwordResetToken }, Request.Scheme); string setPasswordTextEmail = string.Format("Hi {0}, You have just been registered to our website by a system administrator. To complete your registration, please set your password by going to: {1}", userDb.UserName, setPasswordAbsoluteUrl); this.smtpService.SendEmail("NewsWebsite Complete Registration", setPasswordTextEmail, registerReporterViewModel.Email); UsersServiceResultDTO addToRoleResultDTO = await this.usersService.AddToRoleAsync(userDb, RoleConstants.Reporter); if (!addToRoleResultDTO.IsSucceed) { base.AddValidationErrorsToModelState(addToRoleResultDTO.ErrorMessages); return(View(registerReporterViewModel)); } UsersServiceResultDTO usersServiceResultDTO = await this.usersService.ConfirmEmailAsync(userDb, await this.usersService.GenerateEmailConfirmationTokenAsync(userDb)); // We need to confirm the email right after the registration of the reporter, //since we have enabled email confirmation (options.SignIn.RequireConfirmedEmail = true;) in the Startup file and the UsersController login won't let us in. if (!usersServiceResultDTO.IsSucceed) { base.AddValidationErrorsToModelState(usersServiceResultDTO.ErrorMessages); return(View(registerReporterViewModel)); } TempData["SuccessMessage"] = "The reporter was created and has received an email to set his password"; return(RedirectToIndexActionInHomeController()); }
public async Task <IActionResult> SetPasswordAsync(SetPasswordViewModel setPasswordViewModel) { if (!ModelState.IsValid) { return(View(setPasswordViewModel)); } User userDb = await usersService.FindByIdAsync(setPasswordViewModel.UserId); UsersServiceResultDTO resetPasswordResultDTO = await usersService.ResetPasswordAsync(userDb, setPasswordViewModel.PasswordResetToken, setPasswordViewModel.Password); if (!resetPasswordResultDTO.IsSucceed) { base.AddValidationErrorsToModelState(resetPasswordResultDTO.ErrorMessages); return(View(setPasswordViewModel)); } TempData["SuccessMessage"] = "Password set successfully!"; return(RedirectToIndexActionInHomeController()); }
public async Task <IActionResult> Register(RegisterUserViewModel userViewModel) { if (!ModelState.IsValid) { return(View(userViewModel)); } User userDb = new User { UserName = userViewModel.Username, Email = userViewModel.Email }; UsersServiceResultDTO registerResultDTO = await usersService.CreateAsync(userDb, userViewModel.Password); if (!registerResultDTO.IsSucceed) { base.AddValidationErrorsToModelState(registerResultDTO.ErrorMessages); return(View(userViewModel)); } string emailConfirmationToken = await usersService.GenerateEmailConfirmationTokenAsync(userDb); string emailConfirmationLink = this.Url.Action(nameof(ConfirmEmail), this.GetControllerName(nameof(UsersController)), new { userDb.Email, emailConfirmationToken }, Request.Scheme); string emailConfirmation = string.Format("Hi {0}. You have just registered to NewsWebsite. To confirm your email address, please go to: {1}", userDb.UserName, emailConfirmationLink); this.smtpService.SendEmail("NewsWebsite Email Confirmation", emailConfirmation, userViewModel.Email); UsersServiceResultDTO addToRoleResultDTO = await this.usersService.AddToRoleAsync(userDb, RoleConstants.NormalUser); if (!addToRoleResultDTO.IsSucceed) { base.AddValidationErrorsToModelState(addToRoleResultDTO.ErrorMessages); return(View(userViewModel)); } TempData["SuccessMessage"] = "User registered successfully! In order to log in, please check the confirmation email that you have just received."; return(base.RedirectToIndexActionInHomeController()); }