예제 #1
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (ModelState.IsValid)
            {  //generate action code per user
                var code = DateTime.Now.ToShortDateString() + Guid.NewGuid().ToString().Replace("-", string.Empty) + cUsers.Email.ToString();

                var count = _transactionRepository.AddUserAccount(cUsers, code);

                if (count > 0)
                {
                    //send confirmation mail here
                    var    webRoot         = _hostingEnvironment.WebRootPath;
                    string webRootPath     = _hostingEnvironment.WebRootPath;
                    string contentRootPath = _hostingEnvironment.ContentRootPath;

                    //var gt = (webRootPath + "\n" + contentRootPath);

                    //var location = new Uri($"{Request.Scheme}://{Request.Host}");
                    //var code = await _userManager.GenerateEmailConfirmationTokenAsync(cUsers);
                    //var callbackUrl = location.AbsoluteUri + strpath.ToString();
                    var callbackUrl = Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new
                    {
                        userId = Security.EncodeTo64(cUsers.Email),
                        code   = Security.EncodeTo64(code)
                    },
                        protocol: HttpContext.Request.Scheme);

                    if (string.IsNullOrWhiteSpace(_hostingEnvironment.WebRootPath))
                    {
                        webRoot = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot");
                    }

                    //var callbackUrl = Url.Action("Account/ConfirmEmail", "Account", null, protocol: HttpContext.Request.Scheme);

                    //Email from Email Template
                    //string Message = "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>";
                    string Message = "Please confirm your account by clicking ";
                    // string body;

                    //Get TemplateFile located at wwwroot/Templates/EmailTemplate/Register_EmailTemplate.html
                    var pathToFile = webRoot
                                     + Path.DirectorySeparatorChar.ToString()
                                     + "Templates"
                                     + Path.DirectorySeparatorChar.ToString()
                                     + "EmailTemplate"
                                     + Path.DirectorySeparatorChar.ToString()
                                     + "Confirm_Account_Registration.html";

                    var subject = "Confirm Account Registration";

                    var builder = new BodyBuilder();

                    using (StreamReader SourceReader = System.IO.File.OpenText(pathToFile))
                    {
                        builder.HtmlBody = SourceReader.ReadToEnd();
                    }
                    //{0} : Subject
                    //{1} : DateTime
                    //{2} : Email
                    //{3} : Username
                    //{4} : Password
                    //{5} : Message
                    //{6} : callbackURL

                    string messageBody = string.Format(builder.HtmlBody,
                                                       subject,
                                                       String.Format("{0:dddd, d MMMM yyyy}", DateTime.Now),
                                                       cUsers.Email, // "*****@*****.**",
                                                       cUsers.Email, // "*****@*****.**",
                                                       "null",
                                                       Message,
                                                       callbackUrl
                                                       );

                    //await _emailSender.SendEmailAsync("*****@*****.**", "Confirm your account",
                    //          $"Please confirm your account by clicking this link: <a href='{callbackUrl}'>link</a>");

                    await _emailSender.SendEmailAsync(cUsers.Email,
                                                      //"cc",
                                                      subject, messageBody);
                }
                return(RedirectToPage("/Users/Index"));
                //return RedirectToPage("/index");
            }
            else
            {
                return(Page());
            }
        }