Exemplo n.º 1
0
        public async Task <ActionResult> Post(EmailMessageViewModel model)
        {
            var user = _context.Users.FirstOrDefault();

            if (user == null)
            {
                return(BadRequest("No user is registered."));
            }

            var requestIsValid = await _verificationService.IsCaptchaValid(model.Token);

            if (requestIsValid)
            {
                List <KeyValuePair <string, string> > variables = new List <KeyValuePair <string, string> >
                {
                    new KeyValuePair <string, string>(Constants.EmailValues.Name, model.Name),
                    new KeyValuePair <string, string>(Constants.EmailValues.ReplyEmail, model.Email),
                    new KeyValuePair <string, string>(Constants.EmailValues.Message, model.Message)
                };
                var message = TemplateReader.GetTemplate(Constants.EmailTemplates.WebsiteMessageTemplate, variables);

                _emailService.Send(user.Email, "Blogcore Message", message, true);
                return(Ok(model));
            }
            return(BadRequest("Request did not pass validation"));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> ResendEmailVerification()
        {
            User user = _context.Users.FirstOrDefault();

            if (user == null)
            {
                return(View(nameof(Register)));
            }

            var token = await _userManager.GenerateEmailConfirmationTokenAsync(user);

            var confirmationLink = Url.Action(nameof(ConfirmEmail), "Account", new { token, email = user.Email }, Request.Scheme);

            //send email
            var variables = new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>(Constants.EmailValues.Name, $"{user.FirstName} {user.LastName}"),
                new KeyValuePair <string, string>(Constants.EmailValues.ConfirmationLink, confirmationLink)
            };
            var message = TemplateReader.GetTemplate(Constants.EmailTemplates.ConfirmationLinkTemplate, variables);

            _emailService.Send(user.Email, "Confirm your email", message, true);

            return(View(nameof(EmailVerificationSent)));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> ForgotPassword(ForgotPasswordViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var user = await _userManager.FindByEmailAsync(model.Email);

            if (user == null)
            {
                return(RedirectToAction(nameof(ForgotPasswordConfirmation)));
            }

            var token = await _userManager.GeneratePasswordResetTokenAsync(user);

            var link = Url.Action(nameof(ResetPassword), "Account", new { token, email = user.Email }, Request.Scheme);

            var variables = new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>(Constants.EmailValues.Name, $"{user.FirstName} {user.LastName}"),
                new KeyValuePair <string, string>(Constants.EmailValues.PasswordResetLink, link)
            };
            var message = TemplateReader.GetTemplate(Constants.EmailTemplates.PasswordResetTemplate, variables);

            _emailService.Send(user.Email, "BlogCore Password Reset", message, true);

            return(RedirectToAction(nameof(ForgotPasswordConfirmation)));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Register(RegisterViewModel model)
        {
            User admin = _context.Users.FirstOrDefault();

            if (admin == null)
            {
                model.RegisterEnabled = true;
                if (model.Password != model.ConfirmPassword)
                {
                    ModelState.TryAddModelError("PasswordMismatch", "The password fields must match.");
                }
                if (!ModelState.IsValid)
                {
                    return(View(model));
                }

                var user = new User
                {
                    FirstName = model.FirstName,
                    LastName  = model.LastName,
                    UserName  = model.Email,
                    Email     = model.Email
                };

                IdentityResult result = await _userManager.CreateAsync(user, model.Password);

                if (!result.Succeeded)
                {
                    foreach (var error in result.Errors)
                    {
                        ModelState.TryAddModelError(error.Code, error.Description);
                    }
                    return(View(model));
                }

                var token = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                var confirmationLink = Url.Action(nameof(ConfirmEmail), "Account", new { token, email = user.Email }, Request.Scheme);

                //send email
                var variables = new List <KeyValuePair <string, string> >
                {
                    new KeyValuePair <string, string>(Constants.EmailValues.Name, $"{user.FirstName} {user.LastName}"),
                    new KeyValuePair <string, string>(Constants.EmailValues.ConfirmationLink, confirmationLink)
                };
                var message = TemplateReader.GetTemplate(Constants.EmailTemplates.ConfirmationLinkTemplate, variables);

                _emailService.Send(user.Email, "Confirm your email", message, true);

                await _userManager.AddToRoleAsync(user, "Visitor");

                return(RedirectToAction(nameof(SuccessRegistration)));
            }

            return(View(model));
        }
Exemplo n.º 5
0
        public void ExtractTemplateResources()
        {
            if (this.document == null)
            {
                throw new InvalidOperationException("There is no document opened in the workspace.");
            }
            var templateId     = this.document.TemplateId;
            var templateReader = new TemplateReader();
            var template       = templateReader.GetTemplate(templateId);

            if (template != null && template.Resources != null &&
                template.Resources.Length > 0)
            {
                // prepare the directory under working directory to store the resources
                var templateResourceDirectory = Path.Combine(
                    this.workingDirectory,
                    string.Format(TemplateTempDirectoryPattern, templateId.ToString().ToUpper().Replace("-", "_")));
                if (Directory.Exists(templateResourceDirectory))
                {
                    Directory.Delete(templateResourceDirectory, true);
                }

                Directory.CreateDirectory(templateResourceDirectory);

                // read all the entries in the zip file, and find the resource items
                using (var templateFileStream = File.OpenRead(template.MDocxTemplateFileName))
                    using (var zipFile = new ZipFile(templateFileStream))
                    {
                        foreach (ZipEntry zipEntry in zipFile)
                        {
                            var zipEntryName = zipEntry.Name.Replace('/', '\\');
                            if (zipEntry.IsFile && template.Resources.Any(r => string.Equals(zipEntryName, r)))
                            {
                                var outputFileName      = Path.Combine(templateResourceDirectory, zipEntryName);
                                var outputDirectoryName = Path.GetDirectoryName(outputFileName);
                                if (!string.IsNullOrEmpty(outputDirectoryName) && !Directory.Exists(outputDirectoryName))
                                {
                                    Directory.CreateDirectory(outputDirectoryName);
                                }
                                using (var zipEntryStream = zipFile.GetInputStream(zipEntry))
                                    using (var outputFileStream = File.OpenWrite(outputFileName))
                                    {
                                        var buffer = new byte[4096];
                                        StreamUtils.Copy(zipEntryStream, outputFileStream, buffer);
                                    }
                            }
                        }
                    }
            }
        }
Exemplo n.º 6
0
        private string Transform(IEnumerable <KeyValuePair <string, string> > parameters)
        {
            var templateReader = new TemplateReader();
            var template       = templateReader.GetTemplate(this.document.TemplateId);

            if (template != null)
            {
                var templateContent = templateReader.GetTemplateContent(template);
                foreach (var kvp in parameters)
                {
                    if (templateContent.IndexOf(kvp.Key, StringComparison.Ordinal) > 0)
                    {
                        templateContent = templateContent.Replace(kvp.Key, kvp.Value);
                    }
                }
                return(templateContent);
            }
            var keyValuePairs = parameters as KeyValuePair <string, string>[] ?? parameters.ToArray();

            return(keyValuePairs.Any(p => p.Key.Equals(Template.MacroDocumentBody))
                       ? keyValuePairs.First(p => p.Key.Equals(Template.MacroDocumentBody)).Value
                       : null);
        }