public async Task <IActionResult> SubmitForgotPassword(ForgotPasswordModel model,
                                                               CancellationToken cancellationToken = default)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.WarningMessage = Messages.InvalidData;

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

            try
            {
                var requestResetPasswordModel = new GoblinIdentityRequestResetPasswordModel
                {
                    Email = model.Email
                };

                var resetPasswordToken =
                    await GoblinIdentityHelper.RequestResetPasswordAsync(requestResetPasswordModel, cancellationToken);

                var resetPasswordMessage = $"Your reset password code is {resetPasswordToken.SetPasswordToken}.";

                if (resetPasswordToken.SetPasswordTokenExpireTime.HasValue)
                {
                    resetPasswordMessage +=
                        $"<br />Code will expire at {resetPasswordToken.SetPasswordTokenExpireTime.Value.ToString("f")}";
                }

                var newEmailModel = new GoblinNotificationNewEmailModel
                {
                    ToEmails = new List <string>
                    {
                        model.Email
                    },
                    Subject  = $"{SystemSetting.Current.ApplicationName} | Reset Password Code",
                    HtmlBody = resetPasswordMessage
                };

                await GoblinNotificationHelper.SendAsync(newEmailModel, cancellationToken);

                return(View("ResetPassword", new ResetPasswordModel
                {
                    Email = model.Email
                }));
            }
            catch (GoblinException e)
            {
                ViewBag.ErrorMessage = e.ErrorModel.Message;

                return(View("ForgotPassword", model));
            }
            catch (Exception e)
            {
                ViewBag.ErrorMessage = e.Message;

                return(View("ForgotPassword", model));
            }
        }
Example #2
0
        public async Task <IActionResult> VerifyEmail(CancellationToken cancellationToken = default)
        {
            var updateIdentityModel = new GoblinIdentityUpdateIdentityModel
            {
                NewUserName = LoggedInUser <GoblinIdentityUserModel> .Current.Data.UserName,
                NewEmail    = LoggedInUser <GoblinIdentityUserModel> .Current.Data.Email
            };

            if (LoggedInUser <GoblinIdentityUserModel> .Current.Data.EmailConfirmedTime.HasValue)
            {
                ViewBag.ErrorMessage = "Your email already verified!";

                return(View("Account", updateIdentityModel));
            }

            try
            {
                var emailConfirmationModel = await GoblinIdentityHelper.RequestConfirmEmailAsync(LoggedInUser <GoblinIdentityUserModel> .Current.Data.Id, cancellationToken).ConfigureAwait(true);

                // Send Email

                var confirmEmailMessage = $"Your verify email code is {emailConfirmationModel.EmailConfirmToken}.";

                if (emailConfirmationModel.EmailConfirmTokenExpireTime.HasValue)
                {
                    confirmEmailMessage += $"<br />Code will expire at {emailConfirmationModel.EmailConfirmTokenExpireTime.Value.ToString("f")}";
                }

                var newEmailModel = new GoblinNotificationNewEmailModel
                {
                    ToEmails = new List <string>
                    {
                        LoggedInUser <GoblinIdentityUserModel> .Current.Data.Email
                    },
                    Subject  = $"{SystemSetting.Current.ApplicationName} | Verify Your Email",
                    HtmlBody = confirmEmailMessage
                };

                await GoblinNotificationHelper.SendAsync(newEmailModel, cancellationToken);

                ViewBag.WarningMessage = "Please check your email inbox to get the Verify Code.";

                return(View());
            }
            catch (GoblinException e)
            {
                ViewBag.ErrorMessage = e.ErrorModel.Message;
            }
            catch (Exception e)
            {
                ViewBag.ErrorMessage = e.Message;
            }

            return(View("Account", updateIdentityModel));
        }
Example #3
0
        public static async Task SendAsync(GoblinNotificationNewEmailModel model, CancellationToken cancellationToken = default)
        {
            ValidationHelper.Validate <GoblinNotificationNewEmailModelValidator, GoblinNotificationNewEmailModel>(model);

            try
            {
                var endpoint = GetRequest(model.LoggedInUserId).AppendPathSegment(GoblinNotificationEndpoints.SendEmail);

                await endpoint
                .PostJsonAsync(model, cancellationToken : cancellationToken)
                .ConfigureAwait(true);
            }
            catch (FlurlHttpException ex)
            {
                await FlurlHttpExceptionHelper.HandleErrorAsync(ex).ConfigureAwait(true);
            }
        }
        public async Task SendAsync(GoblinNotificationNewEmailModel model, CancellationToken cancellationToken = default)
        {
            var emailMessage = new MimeMessage();

            emailMessage.From.Add(new MailboxAddress(SystemSetting.Current.EmailSenderDisplayName, SystemSetting.Current.EmailSenderEmailAddress));

            emailMessage.To.AddRange(model.ToEmails.Select(x => new MailboxAddress(x, x)));

            if (model.CcEmails?.Any() == true)
            {
                emailMessage.Cc.AddRange(model.CcEmails.Select(x => new MailboxAddress(x, x)));
            }

            if (model.BccEmails?.Any() == true)
            {
                emailMessage.Bcc.AddRange(model.BccEmails.Select(x => new MailboxAddress(x, x)));
            }

            emailMessage.Subject = model.Subject;

            emailMessage.Body = new TextPart("html")
            {
                Text = model.HtmlBody
            };

            using var client = new SmtpClient();

            // Convert System SecureSocketOption to Mail kit SecureSocketOptions
            var secureSocketOptions = (SecureSocketOptions)((int)SystemSetting.Current.EmailSmtpSecureSocketOption);

            await client.ConnectAsync(SystemSetting.Current.EmailSmtpHost, SystemSetting.Current.EmailSmtpPort, secureSocketOptions, cancellationToken).ConfigureAwait(true);

            await client.AuthenticateAsync(SystemSetting.Current.EmailSmtpAccountUserName, SystemSetting.Current.EmailSmtpAccountPassword, cancellationToken).ConfigureAwait(true);

            await client.SendAsync(emailMessage, cancellationToken).ConfigureAwait(true);
        }
        public async Task <IActionResult> Upload([FromBody] GoblinNotificationNewEmailModel model, CancellationToken cancellationToken = default)
        {
            await _emailService.SendAsync(model, cancellationToken);

            return(NoContent());
        }