Пример #1
0
        public async Task EnsureSeedDataAsync()
        {
            var userFound = await m_userManager.FindByEmailAsync("*****@*****.**");
            if (userFound == null)
            {
                var newUser = new ChirpUser()
                {
                    UserName = "******",
                    Email = "*****@*****.**"
                };

                var userRes = await m_userManager.CreateAsync(newUser, "qwertyui");
            }

            m_context.SaveChanges();

            if (!m_context.ChirpPosts.Any())
            {
                //Add new data
                var chirpPost = new ChirpPost()
                {
                    Message = "First message! ChirpChirp!",
                    PostTime = DateTimeOffset.UtcNow,
                    User = await m_userManager.FindByEmailAsync("*****@*****.**")
                };

                m_context.ChirpPosts.Add(chirpPost);

                m_context.SaveChanges();
            }
        }
Пример #2
0
        public async Task<JsonResult> Post([FromBody]SignupViewModel vm, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return Json(new { error = "Unknown sign up error." });
            }

            if (vm.Password != vm.ConfirmPassword)
            {
                return Json(new { error = "The passwords you entered did not match." });
            }

            var userFound = await m_userManager.FindByNameAsync(vm.Username);
            if (userFound != null)
            {
                return Json(new { error = "This username is already in use." });
            }

            userFound = await m_userManager.FindByEmailAsync(vm.Email);
            if (userFound != null)
            {
                return Json(new { error = "This email address is already in use." });
            }

            var newUser = new ChirpUser()
            {
                UserName = vm.Username,
                Email = vm.Email
            };

            var signUpResult = await m_userManager.CreateAsync(newUser, vm.Password);
            if (!signUpResult.Succeeded)
            {
                if (signUpResult.Errors.Any())
                {
                    if (signUpResult.Errors.First().Code == "InvalidUserName")
                    {
                        return Json(new { error = "Invalid username. Usernames can only contain letters, numbers, and .-_" });
                    }
                }
                return Json(new { error = "Unknown sign up error." });
            }

            await SendConfirmationEmail(newUser);

            return Json(new { url = "confirmemailsent" });
        }
Пример #3
0
 public async Task SendConfirmationEmail(ChirpUser a_user)
 {
     var code = await m_userManager.GenerateEmailConfirmationTokenAsync(a_user);
     var callbackUrl = Url.Action("EmailConfirmed", "Auth", new { userId = a_user.Id, code = code }, protocol: HttpContext.Request.Scheme);
     var emailBody = $"Please confirm your account by clicking this link: <br/> <a href=\"{callbackUrl}\"> {callbackUrl} </a>";
     await m_emailSender.SendEmailAsync(a_user.Email, "Confirm your account", emailBody);
 }