public async Task <ActionResult> ActivateAccount(ActivateAccountViewModel model) { var user = await UserManager.FindByNameAsync(model.Email); if (user == null) { ModelState.AddModelError("", "User does not exists"); return(View(model)); } if (!(await UserManager.IsEmailConfirmedAsync(user.Id))) { var subject = "Account Activation"; Random r = new Random(); int otp = 0; for (int i = 1; i < 100; i++) { otp = r.Next(10000, 99999); } var body = otp + " is an OTP to activate your account. "; TempData["ActivateOTP"] = otp; TempData["ActivateEmail"] = model.Email; SendMailTo(body, subject, model.Email); // Don't reveal that the user does not exist or is not confirmed return(RedirectToAction("VerifyAccountOTP", "Activeaccount")); } else { ModelState.AddModelError("", "Your account is already active"); return(View(model)); } }
public async Task <IActionResult> ActivateAccount(ActivateAccountViewModel model) { if (ModelState.IsValid) { try { var user = await _userManager.FindByEmailAsync(model.Email); if (user == null) { throw new ApplicationException($"Unable to load user with ID '{model.Email}'."); } var result = await _userManager.ConfirmEmailAsync(user, model.Code); if (result.Succeeded) { return(Ok()); } } catch (Exception ex) { Log.Error($"Can't activate account for { model.Email } with code { model.Code } ex: { ex.Message }"); } } return(BadRequest(new { message = "An error occured" })); }
public async Task <IActionResult> ActivateAccount(ActivateAccountViewModel model) { if (ModelState.IsValid) { var user = await _userManager.FindByEmailAsync(model.Email); if (user != null) { // Ensure the user account matches the confirmation token var confirmationToken = Encoding.UTF8.GetString(Convert.FromBase64String(model.ConfirmationToken)); if (user.ConfirmationToken == confirmationToken) { // Update EmailConfirmed var result = await _platoUserManager.ConfirmEmailAsync(model.Email, confirmationToken); if (!result.Succeeded) { foreach (var error in result.Errors) { ViewData.ModelState.AddModelError(string.Empty, error.Description); } } return(RedirectToLocal(Url.Action("ActivateAccountConfirmation"))); } } } // If we reach this point the found user's confirmation token does not match the supplied confirmation code ViewData.ModelState.AddModelError(string.Empty, "The email address does not match the confirmation token"); return(await ActivateAccount(model.ConfirmationToken)); }
public async Task <ActivateAccountResultViewModel> ActivateAccount(ActivateAccountViewModel model) { var request = _mapper.Map <ActivateUserRequest>(model); var response = await _bus.RequestAsync(request); if (response.IsSuccess) { return(new ActivateAccountResultViewModel { Success = true, Message = "Je account is geactiveerd!" }); } if (response.ErrorType == ErrorType.TokenInvalid || response.ErrorType == ErrorType.TokenExpired) { return(new ActivateAccountResultViewModel { Message = "Je account kon niet worden geactiveerd omdat de gebruikte link ongeldig is. Vraag een nieuwe activatie-email aan en probeer het opnieuw" }); } return(new ActivateAccountResultViewModel { Message = "Er is een fout opgetreden bij het activeren van je account. Probeer het later opnieuw!" }); }
public IActionResult ActivateAccount(ActivateAccountViewModel model) { if (!ModelState.IsValid) { return(string.IsNullOrWhiteSpace(model.VerificationCode) ? View("Error") : View(model)); } var user = UserManager.GetByVerificationKey(model.VerificationCode); if (user == null) { return(View("Error")); } if (user.IsAccountVerified) { return(User.Identity.IsAuthenticated ? TenantRedirectHelper.RedirectToTenantDomain(user.Tenant, "Index", "Home", Request, Url) : TenantRedirectHelper.RedirectToTenantDomain(user.Tenant, "Login", Request, Url)); } UserManager.VerifyEmailFromKey(model.VerificationCode, model.Password, out user); SignInManager.SignIn(user); return(TenantRedirectHelper.RedirectToTenantDomain(user.Tenant, "Index", "Home", Request, Url)); }
public ActionResult Activate(ActivateAccountViewModel model) { if (!string.IsNullOrEmpty(model.Email) && !string.IsNullOrEmpty(model.ActivationKey)) { var user = ApplicationUserEngine.GetApplicationUser(model.Email); if (user != null) { if (!string.IsNullOrEmpty(user.ActivationKey)) { if (user.ActivationKey == model.ActivationKey) { user.ActivationKey = ""; ApplicationUserEngine.ActivateUser(user.Email); ApplicationUserEngine.UpdateUserInCache(user.Email); return(View("ActivationSuccess")); } ModelState.AddModelError("", "The activation key you provided is not correct."); } else { ModelState.AddModelError("", "This account has already been activated."); } } else { ModelState.AddModelError("", "Email address not associated with an account."); } } else { ModelState.AddModelError("", "Please fill out both form fields."); } return(View(model)); }