public IActionResult New(string username, string email, string password)
        {
            ViewBag.Error = new List <string>();
            if (!validator.IsValidUsername(username))
            {
                ViewBag.Error.Add("Invalid username. Usernames can only contain the characters a-z, A-Z, 0-9, _ and -.");
            }
            if (!validator.IsValidEmail(email))
            {
                ViewBag.Error.Add("Invalid email address.");
            }
            if (userRepository.FindByUsername(username) != null)
            {
                ViewBag.Error.Add("The username is already taken.");
            }
            if (userRepository.FindByEmail(email) != null)
            {
                ViewBag.Error.Add("The email address is already taken.");
            }
            if (ViewBag.Error.Count > 0)
            {
                return(View("Register"));
            }
            else
            {
                ViewBag.Error = null;
            }
            Tuple <string, string> hashAndSalt = passwordHasher.HashPassword(password);
            string hash   = hashAndSalt.Item1;
            string salt   = hashAndSalt.Item2;
            int    userId = counterRepository.GetAndIncrease(Counter.USER_ID);
            User   user   = new User(userId, username, email, hash, salt, "", 0, 0,
                                     new List <string>()
            {
                UserRole.NONE
            }, new Dictionary <string, Rating>()
            {
                { "Atomic", new Rating(1500, 350, 0.06) },
                { "ThreeCheck", new Rating(1500, 350, 0.06) },
                { "KingOfTheHill", new Rating(1500, 350, 0.06) },
                { "Antichess", new Rating(1500, 350, 0.06) },
                { "Horde", new Rating(1500, 350, 0.06) },
                { "RacingKings", new Rating(1500, 350, 0.06) }
            }, new List <int>());
            bool added = userRepository.Add(user);

            userVerifier.SendVerificationEmailTo(user.ID);
            loginHandler.RegisterLogin(user.ID, HttpContext);
            return(RedirectToAction("Profile", new { id = user.ID }));
        }
Пример #2
0
        public async Task <IActionResult> New(string username, string email, string password, string passwordConfirmation, [FromForm(Name = "g-recaptcha-response")] string gRecaptchaResponse)
        {
            ViewBag.Error = new List <string>();
            if (!validator.IsValidUsername(username))
            {
                ViewBag.Error.Add("Invalid username. Usernames can only contain the characters a-z, A-Z, 0-9, _ and -.");
            }
            if (!validator.IsValidEmail(email))
            {
                ViewBag.Error.Add("Invalid email address.");
            }
            if (userRepository.FindByUsername(username) != null)
            {
                ViewBag.Error.Add("The username is already taken.");
            }
            if (userRepository.FindByEmail(email) != null)
            {
                ViewBag.Error.Add("The email address is already taken.");
            }

            if (string.IsNullOrEmpty(password) || string.IsNullOrEmpty(passwordConfirmation))
            {
                ViewBag.Error.Add("Your password or its confirmation cannot be empty.");
            }
            else if (!password.Equals(passwordConfirmation))
            {
                ViewBag.Error.Add("The password does not match its confirmation.");
            }

            if (!string.IsNullOrWhiteSpace(recaptchaKey))
            {
                Dictionary <string, string> captchaRequestValues = new Dictionary <string, string>()
                {
                    { "secret", recaptchaKey },
                    { "response", gRecaptchaResponse }
                };
                FormUrlEncodedContent content  = new FormUrlEncodedContent(captchaRequestValues);
                HttpResponseMessage   response = await captchaClient.PostAsync("https://www.google.com/recaptcha/api/siteverify", content);

                string responseString = await response.Content.ReadAsStringAsync();

                Dictionary <string, dynamic> jsonResponse = JsonConvert.DeserializeObject <Dictionary <string, dynamic> >(responseString);
                if (!((bool)jsonResponse["success"]))
                {
                    ViewBag.Error.Add("Captcha verification failed.");
                }
            }

            if (ViewBag.Error.Count > 0)
            {
                return(View("Register"));
            }
            else
            {
                ViewBag.Error = null;
            }
            Tuple <string, string> hashAndSalt = passwordHasher.HashPassword(password);
            string hash   = hashAndSalt.Item1;
            string salt   = hashAndSalt.Item2;
            int    userId = counterRepository.GetAndIncrease(Counter.USER_ID);
            User   user   = new User(userId, username, email, hash, salt, "", 0, 0,
                                     new List <string>()
            {
                UserRole.NONE
            }, new Dictionary <string, Rating>()
            {
                { "Atomic", new Rating(1500, 350, 0.06) },
                { "ThreeCheck", new Rating(1500, 350, 0.06) },
                { "KingOfTheHill", new Rating(1500, 350, 0.06) },
                { "Antichess", new Rating(1500, 350, 0.06) },
                { "Horde", new Rating(1500, 350, 0.06) },
                { "RacingKings", new Rating(1500, 350, 0.06) },
                { "Crazyhouse", new Rating(1500, 350, 0.06) }
            }, new List <int>());

            if (!requireEmailVerification)
            {
                user.VerificationCode = 0;
                user.Verified         = true;
            }
            bool added = userRepository.Add(user);

            if (requireEmailVerification)
            {
                userVerifier.SendVerificationEmailTo(user.ID);
            }
            loginHandler.RegisterLogin(user.ID, HttpContext);
            return(RedirectToAction("Profile", new { id = user.ID }));
        }