public async Task <IActionResult> OnPostAsync() { try { if (ModelState.IsValid) { var user = await _userManager.FindByEmailAsync(Input.Email); if (user == null || !(await _userManager.IsEmailConfirmedAsync(user))) { _logger.LogWarning("Nenhum usuário encontrado!"); // Don't reveal that the user does not exist or is not confirmed return(RedirectToPage("./ForgotPasswordConfirmation")); } _logger.LogInformation("Criando corpo de e-mail"); var pathToFile = _env.WebRootPath + Path.DirectorySeparatorChar.ToString() + "templates" + Path.DirectorySeparatorChar.ToString() + "PasswordRecoveryEmail.html"; var builder = new BodyBuilder(); using (StreamReader sourceReader = System.IO.File.OpenText(pathToFile)) { builder.HtmlBody = sourceReader.ReadToEnd(); } // For more information on how to enable account confirmation and password reset please // visit https://go.microsoft.com/fwlink/?LinkID=532713 var code = await _userManager.GeneratePasswordResetTokenAsync(user); var callbackUrl = Url.Page( "/Account/ResetPassword", pageHandler: null, values: new { code }, protocol: Request.Scheme); string messageBody = string.Format(builder.HtmlBody, callbackUrl); _logger.LogInformation("Criando Email"); var mail = new MensagemEmail() { Assunto = "Email de Recuperação de senha", Conteudo = messageBody }; mail.DeEndereco.Add(new EnderecoEmail() { Nome = "FalzoGamer", Endereco = "*****@*****.**" }); mail.ParaEndereco.Add(new EnderecoEmail() { Nome = user.FirstName + " " + user.LastName, Endereco = Input.Email }); _logger.LogInformation("Enviando Email"); await _emailServico.SendAsync(mail); return(RedirectToPage("./ForgotPasswordConfirmation")); } return(Page()); } catch (Exception ex) { _logger.LogError("Ocorreu o seguinte erro: " + ex.Message); return(Page()); } }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (ModelState.IsValid) { var user = new ApplicationUser { UserName = Input.Email, Email = Input.Email }; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { _logger.LogInformation("Usuário criado com sucesso!"); _logger.LogInformation("Criando corpo de e-mail"); var pathToFile = _env.WebRootPath + Path.DirectorySeparatorChar.ToString() + "templates" + Path.DirectorySeparatorChar.ToString() + "EmailConfirmation.html"; var builder = new BodyBuilder(); using (StreamReader sourceReader = System.IO.File.OpenText(pathToFile)) { builder.HtmlBody = sourceReader.ReadToEnd(); } var codigo = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { userId = user.Id, code = codigo }, protocol: Request.Scheme); string messageBody = string.Format(builder.HtmlBody, callbackUrl); _logger.LogInformation("Criando Email"); var mail = new MensagemEmail() { Assunto = "Confirmação de Email", Conteudo = messageBody }; mail.DeEndereco.Add(new EnderecoEmail() { Nome = "FalzoGamer", Endereco = "*****@*****.**" }); mail.ParaEndereco.Add(new EnderecoEmail() { Nome = user.FirstName + " " + user.LastName, Endereco = Input.Email }); _logger.LogInformation("Enviando Email"); await _emailServico.SendAsync(mail); await _signInManager.SignInAsync(user, isPersistent : false); return(LocalRedirect(returnUrl)); } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(Page()); }