public async Task RequestPasswordReset(ForgotPasswordDto forgotPasswordDto)
        {
            try
            {
                var user = await _userManager.FindByEmailAsync(forgotPasswordDto.Email);

                if (user == null)
                {
                    Status.AddError("A user was not found with the 'email address' provided");
                }
                if (Status.HasErrors)
                {
                    return;
                }

                var passwordResetToken = await _userManager.GeneratePasswordResetTokenAsync(user);

                passwordResetToken = HttpUtility.UrlEncode(passwordResetToken);

                var forgotPasswordEmail =
                    _accountEmailsService.ForgotPasswordEmail(user.FirstName, user.Id,
                                                              passwordResetToken);

                await _sendEmailService.SendAsync(user.Email, "Password Reset", forgotPasswordEmail, user.FirstName);

                if (_sendEmailService.Status.HasErrors)
                {
                    Status.CombineErrors(_sendEmailService.Status);
                }
            }
            catch (Exception e)
            {
                Status.AddError(e.Message);
            }
        }
Esempio n. 2
0
        public async Task Handle(Notification notification, CancellationToken cancellationToken)
        {
            var emailData = new EmailMessage()
            {
                To      = notification.Emails.ToArray(),
                Subject = notification.Subject,
                Body    = $"{notification.Body}"
            };

            await _sendEmailService.SendAsync(emailData);
        }
        public async Task RegisterAccount(RegisterDto registerDto)
        {
            using (var scope = _context.Database.BeginTransaction())
            {
                try
                {
                    var user        = _mapper.Map <RegisterDto, ApplicationUser>(registerDto);
                    var userCreated = await _userManager.CreateAsync(user, registerDto.Password);

                    if (userCreated.Succeeded)
                    {
                        var emailConfirmationToken = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                        emailConfirmationToken = HttpUtility.UrlEncode(emailConfirmationToken);

                        var confirmationEmail =
                            _confimationEmailService.ConfirmationEmail(registerDto.FirstName, user.Id, emailConfirmationToken);

                        await _sendEmailService.SendAsync(user.Email, "Confirm your account", confirmationEmail,
                                                          registerDto.FirstName);

                        if (_sendEmailService.Status.HasErrors)
                        {
                            Status.CombineErrors(_sendEmailService.Status);
                        }
                        if (Status.HasErrors)
                        {
                            return;
                        }

                        await _context.SaveChangesAsync();

                        scope.Commit();
                    }

                    foreach (var error in userCreated.Errors)
                    {
                        Status.AddError(error.Description);
                    }
                }
                catch (Exception e)
                {
                    Status.AddError(e.Message);
                    scope.Rollback();
                    await _errorFactory.LogError(e.Message, "RegisterAccount", _context);
                }
            }
        }
Esempio n. 4
0
        public async Task <IActionResult> ForgotPassword(ForgotPasswordDTO ForgotPassword)
        {
            var captchaText = TextTools.GetEnglishNumber(ForgotPassword.txtCaptcha);
            var session     = HttpContext.Session.GetString("Captcha");

            if (string.IsNullOrEmpty(session) || session != captchaText)
            {
                ModelState.AddModelError("txtCaptcha", "کد امنیتی را اشتباه وارد کردید");
            }

            if (ModelState.IsValid)
            {
                var user = await userManager.FindByEmailAsync(ForgotPassword.UserName);

                if (user is null)
                {
                    ModelState.AddModelError("UserName", "  کاربری با این مشخصات یافت نشد .");
                    return(View(ForgotPassword));
                }
                var Token = await userManager.GeneratePasswordResetTokenAsync(user);

                var request = HttpContext.Request;
                var Domain  = $"{request.Scheme}://{request.Host}";

                var emailContent = new EmailContentDto()
                {
                    FirstName = user.FirstName,
                    LastName  = user.LastName,
                    Link      = $"{Domain}/Account/ResetPassword?username={user.UserName}&token={Token}&UCB={ForgotPassword.UCB}"
                };
                var Body = await this.RenderViewAsync("_ResetPasswordEmailContent", emailContent);

                await sendEmailService.SendAsync(user.Email, Body);

                return(RedirectToAction("ResponseForgotPassword"));
            }

            return(View(ForgotPassword));
        }
Esempio n. 5
0
        /// <summary>
        /// Method to send platform invitation email.
        /// </summary>
        /// <param name="recipientEmail">The email address of the recipient.</param>
        public Task <bool> SendInvitationAsync(MailAddress recipientEmailAddress, CancellationToken cancellationToken)
        {
            var registrationLink = new UriBuilder(ConfigurationManager.AppSettings["AzurePlatform:ApplicationGateway:FQDN"])
            {
                Path = "/members/register"
            };

            var bodyText = new StringBuilder(_localizationService.GetResourceString("Email.Invite.Body"));

            bodyText
            .Append("<br><br><a href='")
            .Append(registrationLink.Uri.ToString())
            .Append("'>")
            .Append(_localizationService.GetResourceString("Email.Invite.Link"))
            .Append("</a>");

            return(_sendEmailService.SendAsync(
                       new MailAddress(_configurationProvider.SmtpFrom),
                       recipientEmailAddress,
                       _localizationService.GetResourceString("Email.Invite.Subject"),
                       bodyText.ToString(),
                       cancellationToken
                       ));
        }
Esempio n. 6
0
        public virtual async Task <ActionResult> ForgotPasswordAsync(ForgotPasswordViewModel forgotPasswordViewModel, CancellationToken cancellationToken)
        {
            if (!ModelState.IsValid)
            {
                return(View(forgotPasswordViewModel));
            }

            var user = MembershipService.GetUserByEmail(forgotPasswordViewModel.EmailAddress);

            // If the email address is not registered then display the 'email sent' confirmation the same as if
            // the email address was registered. There is no harm in doing this and it avoids exposing registered
            // email addresses which could be a privacy issue if the forum is of a sensitive nature. */
            if (user == null)
            {
                return(RedirectToAction("PasswordResetSent", "Members"));
            }

            try
            {
                // If the user is registered then create a security token and a timestamp that will allow a change of password
                MembershipService.UpdatePasswordResetToken(user);
                Context.SaveChanges();
            }
            catch (Exception ex)
            {
                Context.RollBack();
                LoggingService.Error(ex);
                ModelState.AddModelError("", LocalizationService.GetResourceString("Members.ResetPassword.Error"));
                return(View(forgotPasswordViewModel));
            }

            var settings = SettingsService.GetSettings();
            var url      = new Uri(string.Concat(settings.ForumUrl.TrimEnd('/'),
                                                 Url.Action("ResetPassword", "Members", new { user.Id, token = user.PasswordResetToken })));

            var sb = new StringBuilder();

            sb.AppendFormat("<p>A request has been made to reset your password. To reset your password follow the link below. If you did not make this request then please ignore this email. No further action is required and your password will not be changed.</p>");
            sb.AppendFormat("<p><a href=\"{0}\">{0}</a></p>", url);

            var emailToAddress   = new MailAddress(user.Email);
            var emailFromAddress = new MailAddress(_configurationProvider.SmtpFrom);
            var emailSubject     = LocalizationService.GetResourceString("Members.ForgotPassword.Subject");
            var emailBodyHtml    = _emailService.EmailTemplate(user.GetFullName(), sb.ToString());

            await _sendEmailService.SendAsync(emailFromAddress, emailToAddress, emailSubject, emailBodyHtml, cancellationToken);

            try
            {
                Context.SaveChanges();
            }
            catch (Exception ex)
            {
                Context.RollBack();
                LoggingService.Error(ex);
                ModelState.AddModelError("", LocalizationService.GetResourceString("Members.ResetPassword.Error"));
                return(View(forgotPasswordViewModel));
            }


            return(RedirectToAction("PasswordResetSent", "Members"));
        }