public virtual Task SendPrivateMessageAsync(User from, User to, string text)
 {
     return(emailSenderService.SendEmailByTemplateAsync(
                to.Email,
                "private-message.html",
                new Dictionary <string, string>
     {
         { "[siteName]", globalOptions.SiteName },
         { "[url]", globalOptions.SiteUrl.AppendPathSegment("user/" + from.Link) },
         { "[userName]", from.UserName },
         { "[message]", sanitizer.Sanitize(text) }
     }
                ));
 }
Example #2
0
        public virtual async Task ResetPasswordSendEmailAsync(User user)
        {
            var resetToken = await userManager.GeneratePasswordResetTokenAsync(user);

            var resetPasswordUrl = globalOptions.SiteApi
                                   .AppendPathSegments("Account", "ResetPasswordShowClientDialog")
                                   .SetQueryParams(new { uid = user.Id, token = resetToken });

            try
            {
                await emailSenderService.SendEmailByTemplateAsync(
                    user.Email,
                    "reset-password.html",
                    new Dictionary <string, string> {
                    { "[resetPassUrl]", resetPasswordUrl }
                }
                    );
            }
            catch (Exception exception)
            {
                throw new SunViewException(new ErrorView("EmailSendError", "Server error. Can not send email.", ErrorType.System, exception));
            }
        }
        public virtual async Task RegisterAsync(NewUserArgs model)
        {
            var user = new User
            {
                UserName       = model.UserName,
                Email          = model.Email,
                Avatar         = User.DefaultAvatar,
                Photo          = User.DefaultAvatar,
                RegisteredDate = DateTime.UtcNow
            };

            using (db.BeginTransaction())
            {
                IdentityResult result = await userManager.CreateAsync(user, model.Password);

                if (!result.Succeeded)
                {
                    // If user already try to register but do not confirmed, try to update data

                    if (!result.Errors.Any(x => x.Code == "DuplicateEmail"))
                    {
                        throw new SunViewException(new ErrorView(result.Errors));
                    }

                    user = await userManager.FindByEmailAsync(model.Email);

                    if (user.EmailConfirmed)
                    {
                        throw new SunViewException(new ErrorView(result.Errors));
                    }

                    user.UserName     = model.UserName;
                    user.PasswordHash = userManager.PasswordHasher.HashPassword(user, model.Password);

                    result = await userManager.UpdateAsync(user);

                    if (!result.Succeeded)
                    {
                        throw new SunViewException(new ErrorView(result.Errors));
                    }
                }
                else
                {
                    await db.Users.Where(x => x.Id == user.Id).Set(x => x.Link, x => x.Id.ToString())
                    .UpdateAsync();

                    logger.LogInformation($"New user registered (id: {user.Id})");
                }

                // Send email confirmation email
                var confirmToken = await userManager.GenerateEmailConfirmationTokenAsync(user);

                var emailConfirmUrl = globalOptions.SiteApi
                                      .AppendPathSegments("Auth", "ConfirmRegister")
                                      .SetQueryParams(new { uid = user.Id, token = confirmToken });

                try
                {
                    await EmailSenderService.SendEmailByTemplateAsync(
                        model.Email,
                        "register.html",
                        new Dictionary <string, string> {
                        { "[link]", emailConfirmUrl }
                    }
                        );
                }
                catch (Exception exception)
                {
                    throw new SunViewException(new ErrorView("EmailSendError", "Can not send email",
                                                             ErrorType.System, exception));
                }


                logger.LogInformation($"Sent email confirmation email (id: {user.Id})");

                db.CommitTransaction();
            }
        }
Example #4
0
        public virtual async Task RegisterAsync(NewUserArgs model)
        {
            var user = new User
            {
                UserName       = model.UserName?.Trim(),
                Email          = model.Email,
                RegisteredDate = DateTime.UtcNow
            };

            using (db.BeginTransaction())
            {
                IdentityResult result = await userManager.CreateAsync(user, model.Password);

                if (!result.Succeeded)
                {
                    var error = result.Errors.FirstOrDefault();

                    if (result.Errors.All(x => x.Code != "DuplicateEmail"))
                    {
                        throw new SunErrorException(new Error(error.Code, error.Description, ErrorType.System));
                    }

                    user = await userManager.FindByEmailAsync(model.Email);

                    if (user.EmailConfirmed)
                    {
                        throw new SunErrorException(new Error(error.Code, error.Description, ErrorType.System));
                    }

                    user.UserName     = model.UserName;
                    user.PasswordHash = userManager.PasswordHasher.HashPassword(user, model.Password);

                    result = await userManager.UpdateAsync(user);

                    if (!result.Succeeded)
                    {
                        throw new SunErrorException(new Error(error.Code, error.Description, ErrorType.System));
                    }
                }
                else
                {
                    await db.Users.Where(x => x.Id == user.Id).Set(x => x.Link, x => x.Id.ToString())
                    .UpdateAsync();

                    logger.LogInformation($"New user registered (id: {user.Id})");
                }

                // Send email confirmation email
                var confirmToken = await userManager.GenerateEmailConfirmationTokenAsync(user);

                var emailConfirmUrl = urlsOptions.CurrentValue.Api
                                      .AppendPathSegments("Auth", "ConfirmRegister")
                                      .SetQueryParams(new { uid = user.Id, token = confirmToken });

                try
                {
                    await emailSenderService.SendEmailByTemplateAsync(
                        model.Email,
                        "register.html",
                        new Dictionary <string, string> {
                        { "[link]", emailConfirmUrl }
                    }
                        );
                }
                catch (Exception exception)
                {
                    throw new SunErrorException(new Error("EmailSendError", "Can not send email",
                                                          ErrorType.System, exception));
                }


                logger.LogInformation($"Sent email confirmation email (id: {user.Id})");

                db.CommitTransaction();
            }
        }