Esempio n. 1
0
        public async Task <int> CreateUserAsync(UserCreateModel model, UserIdentity <int> issuer)
        {
            model.Status = UserStatus.Active;

            var user = _mapper.Map <User>(model);

            user.CreateBy(issuer).UpdateBy(issuer);

            model.Password = AutoGenerate.AutoGeneratePassword(8, true, true);

            var identityResult = await _userManagement.CreateAsync(user, model.Password);

            if (!identityResult.Succeeded)
            {
                throw CreateException(identityResult.Errors);
            }

            IdentityResult resultRole = await _userManagement.AddToRoleAsync(user, model.Role);

            EmailTemplate emailTemplate = _emailTemplateRepository.GetEmailTemplateByName("NewUserEmail");

            emailTemplate.EmailContent = emailTemplate.EmailContent.Replace("#email", model.Email);
            emailTemplate.EmailContent = emailTemplate.EmailContent.Replace("#username", model.UserName);
            emailTemplate.EmailContent = emailTemplate.EmailContent.Replace("#password", model.Password);
            try
            {
                await _emailSender.SendEmailAsync(model.Email, emailTemplate.EmailSubject, emailTemplate.EmailContent, true);
            }
            catch (Exception e)
            {
                throw e;
            }

            return(user.Id);
        }
        public async Task SendAddNewUserNotification(User user)
        {
            string fromAddress = _settingRepository.GetSettingByKey <string>(EmailFromAddress, ""),
                   toAddresses = _settingRepository.GetSettingByKey <string>(EmailToAddresses, ""),
                   smtpHost    = _settingRepository.GetSettingByKey <string>(EmailSmtpHost, ""),
                   websiteUrl  = _settingRepository.GetSettingByKey <string>(WebsiteUrl, "");

            if (string.IsNullOrWhiteSpace(fromAddress) || string.IsNullOrWhiteSpace(toAddresses) ||
                string.IsNullOrWhiteSpace(smtpHost) || string.IsNullOrWhiteSpace(websiteUrl))
            {
                throw new Exception("Email configuration hasn't been set up yet.");
            }

            var toAddressCollection = toAddresses.Split(new[] { ",", ";" }, StringSplitOptions.RemoveEmptyEntries).ToList();
            var emailAccount        = new EmailAccount {
                Host = smtpHost
            };

            var template = await _emailTemplateRepository.GetEmailTemplateByName(
                Constants.EmailTemplates.AddNewUserNotification);

            string subject = ReplaceMessageTemplateTokens(user, websiteUrl, template.Subject);
            string body    = ReplaceMessageTemplateTokens(user, websiteUrl, template.Body);

            await _emailSender.SendEmailAsync(emailAccount, subject, body, fromAddress, toAddressCollection);
        }
Esempio n. 3
0
 public EmailTemplate GetEmailTemplateByName(string name)
 {
     return(_repository.GetEmailTemplateByName(name));
 }
Esempio n. 4
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="templateName"></param>
 /// <returns></returns>
 public async Task <EmailTemplate> GetEmailTemplateByName(string templateName)
 {
     return(await _repository.GetEmailTemplateByName(templateName));
 }