Exemplo n.º 1
0
        /// <summary>
        /// Executed on "PasswordReset" Click by user xy...
        /// </summary>
        /// <param name="userVm">Userobject</param>
        public bool SendPasswortRecoveryForUser(UserViewModel userVm)
        {
            try
            {
                var pwrModel = new PasswordResetModel()
                {
                    Email          = userVm.Email,
                    TokenHash      = GetNewHashToken(),
                    ExpirationDate = DateTime.Now.AddDays(1).ToString("yyyyMMddHHmmss"),
                    TokenUsed      = 0
                };

                _context.Add(_mapper.Map <PasswordReset>(pwrModel));
                _context.Save();

                //Finally
                SendMailWithResetLink(userVm.Email, pwrModel.TokenHash);
            }catch (Exception ex)
            {
                return(false);
            }

            return(true);
        }
Exemplo n.º 2
0
        public IActionResult Send(ForgotPasswordViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    Participant participant = _participantRepository.FindUniqueByEmail(model.Email);

                    if (participant == null)
                    {
                        ModelState.AddModelError("Email", "E-mail não encontrado na base de dados.");

                        return(View("Index", model));
                    }

                    StringBuilder builder = new StringBuilder();
                    builder.Append(RandomString(4, true));
                    string hash = HashExtension.Create(builder.ToString(), Environment.GetEnvironmentVariable("AUTH_SALT"));
                    hash = hash.Replace(" ", String.Empty);

                    PasswordReset passwordReset = new PasswordReset
                    {
                        Email = model.Email,
                        Token = hash
                    };

                    PasswordReset old = _passwordResetRepository.FindUniqueByEmail(model.Email);

                    if (old != null)
                    {
                        _passwordResetRepository.Remove(old.Id);
                    }

                    _passwordResetRepository.Add(passwordReset);
                    _passwordResetRepository.SaveChanges();

                    var message = new MimeMessage();
                    message.To.Add(new MailboxAddress(participant.Name, model.Email));
                    message.From.Add(new MailboxAddress("Contact Promotion", "*****@*****.**"));
                    message.Subject = "Promotion - Reset Password";
                    message.Body    = new TextPart(TextFormat.Html)
                    {
                        Text = "<strong>Olá!</strong>" + "<br>Clique no link para recuperar sua senha: " +
                               "<a href='https://localhost:5001/participant/reset-password?email=" + model.Email + "&token=" + hash + "' target='_blank'>Recuperar senha</a>"
                    };

                    using (var client = new SmtpClient())
                    {
                        client.Connect("smtp.mailtrap.io", 587, false);
                        client.Authenticate("", "");
                        client.Send(message);
                        client.Disconnect(true);
                    }

                    TempData["Success"] = "Cheque sua caixa de e-mail!";

                    return(RedirectToAction("Index"));
                }
            }
            catch (Exception exception)
            {
                _logger.LogError("Contact send error: " + exception);
                return(StatusCode(500));
            }

            return(View("Index", model));
        }