public async Task <IActionResult> RecoverPassword([FromBody] UserPasswordRecovery recoveryInfo)
        {
            if (ModelState.IsValid)
            {
                ApplicationUser appUser = null;

                if (recoveryInfo.UsernameOrEmail.Contains("@"))
                {
                    appUser = await _accountManager.GetUserByEmailAsync(recoveryInfo.UsernameOrEmail);
                }

                if (appUser == null)
                {
                    appUser = await _accountManager.GetUserByUserNameAsync(recoveryInfo.UsernameOrEmail);
                }

                if (appUser == null || !(await _accountManager.IsEmailConfirmedAsync(appUser)))
                {
                    // Don't reveal that the user does not exist or is not confirmed
                    return(Accepted());
                }

                string code = await _accountManager.GeneratePasswordResetTokenAsync(appUser);

                string callbackUrl = $"{Request.Scheme}://{Request.Host}/ResetPassword?code={code}";
                string message     = EmailTemplates.GetResetPasswordEmail(appUser.UserName, HtmlEncoder.Default.Encode(callbackUrl));

                await _emailSender.SendEmailAsync(appUser.UserName, appUser.Email, "Reset Password", message);

                return(Accepted());
            }

            return(BadRequest(ModelState));
        }
            public async Task <Unit> Handle(Query request, CancellationToken cancellationToken)
            {
                var user = await _userManager.FindByEmailAsync(request.Email);

                var token = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                //ENcode the token with webEncoder
                token = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(token));
                var verifyUrl = $"{request.Origin}/user/verifyEmail?token={token}&email={request.Email}";
                var message   = $"<p>Please click the below link to verify you email adress: <a href='{verifyUrl}'>{verifyUrl}<a/></p>";
                await _emailSender.SendEmailAsync(request.Email, "Verify adress", message);

                return(Unit.Value);
            }
Example #3
0
        public async Task<string> Email()
        {
            string recepientName = "QickApp Tester"; //         <===== Put the recepient's name here
            string recepientEmail = "*****@*****.**"; //   <===== Put the recepient's email here

            string message = EmailTemplates.GetTestEmail(recepientName, DateTime.UtcNow);

            (bool success, string errorMsg) = await _emailSender.SendEmailAsync(recepientName, recepientEmail, "Test Email from QuickApp", message);

            if (success)
                return "Success";

            return "Error: " + errorMsg;
        }
        public async Task SendEMail(string tenantId, IJobCancellationToken cancellationToken)
        {
            PagedResult <NotificationMessageDto> pagedResult;

            do
            {
                pagedResult = await _notificationRepository.GetPendingMessagesAsync(tenantId, NotificationTypesDto.EMail, 20);

                if (pagedResult.TotalCount == 0)
                {
                    break;
                }

                foreach (var notificationMessageDto in pagedResult.List)
                {
                    notificationMessageDto.LastTryDateTime = DateTime.UtcNow;
                }

                await _notificationRepository.StoreNotificationMessages(tenantId, pagedResult.List);

                cancellationToken?.ThrowIfCancellationRequested();

                foreach (var notificationMessageDto in pagedResult.List)
                {
                    try
                    {
                        await _eMailSender.SendEmailAsync(notificationMessageDto.RecipientAddress,
                                                          notificationMessageDto.SubjectText,
                                                          notificationMessageDto.BodyText);

                        notificationMessageDto.SentDateTime = DateTime.UtcNow;
                        notificationMessageDto.SendStatus   = SendStatusDto.Sent;
                        notificationMessageDto.ErrorText    = null;
                    }
                    catch (NotificationSendFailedException e)
                    {
                        notificationMessageDto.SentDateTime = DateTime.UtcNow;
                        notificationMessageDto.SendStatus   = SendStatusDto.Error;
                        notificationMessageDto.ErrorText    = e.GetDirectAndIndirectMessages();
                    }
                }

                await _notificationRepository.StoreNotificationMessages(tenantId, pagedResult.List);
            } while (pagedResult.TotalCount > 0);
        }
Example #5
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                if (await context.Users.AnyAsync(x => x.Email == request.Email))
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { Email = "Email already exists" });
                }
                if (await context.Users.AnyAsync(x => x.UserName == request.Username))
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { Username = "******" });
                }

                var user = new UserFasserly
                {
                    UserName    = request.Username,
                    Email       = request.Email,
                    DisplayName = request.DisplayName,
                };

                var result = await _userManager.CreateAsync(user, request.Password);

                await _userManager.UpdateAsync(user);

                if (!result.Succeeded)
                {
                    throw new Exception("Problem creating user");
                }
                var token = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                //ENcode the token with webEncoder
                token = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(token));
                var verifyUrl = $"{request.Origin}/user/verifyEmail?token={token}&email={request.Email}";
                var message   = $"<p>Please click the below link to verify you email adress: <a href='{verifyUrl}'>{verifyUrl}<a/></p>";
                await _eMailSender.SendEmailAsync(request.Email, "Verify adress", message);

                return(Unit.Value);
            }