Ejemplo n.º 1
0
        public async Task <IActionResult> AddUser([FromBody] RegisterViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var user = await userManager.FindByEmailAsync(model.Username);

                    if (user == null)
                    {
                        user = new LotteryGeniusUser()
                        {
                            UserName  = model.Username,
                            Email     = model.Username,
                            FirstName = model.FirstName,
                            LastName  = model.LastName
                        };

                        var userResult = await userManager.CreateAsync(user, model.LastName + DateTime.Now.Year + "!");

                        if (userResult == IdentityResult.Success)
                        {
                            await userManager.AddToRoleAsync(user, model.Role);

                            user.initialLogin = true;
                            _ctx.SaveChanges();

                            var webRoot    = this.env.WebRootPath;
                            var pathToFile = this.env.WebRootPath + Path.DirectorySeparatorChar.ToString() + "Templates"
                                             + Path.DirectorySeparatorChar.ToString() + "lottonewuser_email.html";
                            var builder = new BodyBuilder();

                            using (StreamReader SourceReader = System.IO.File.OpenText(pathToFile))
                            {
                                builder.HtmlBody = SourceReader.ReadToEnd();
                            }

                            string messageBody = builder.HtmlBody;
                            messageBody = messageBody.Replace("{user}", $"{model.FirstName} {model.LastName}")
                                          .Replace("{username}", $"{model.Username}").Replace("{password}", $"{model.LastName + DateTime.Now.Year}!").Replace("{url}", $"{this.config["WebsiteOrigin"]}/Home/EmailVerification/?email={model.Username}");

                            await _emailSender.SendEmailAsync(user.Email, "Email Verification", messageBody);

                            return(Ok());
                        }
                    }
                }
                else
                {
                    return(BadRequest(ModelState));
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }

            return(BadRequest("Failed to save user data"));
        }
Ejemplo n.º 2
0
 public bool SaveAll()
 {
     return(_ctx.SaveChanges() > 0);
 }