Пример #1
0
        public UserData(string userName, bool validateUserExists = false) : base(new ActivityContext(new ActivityPrincipal(new ActivityIdentity(userName))))
        {
            System.Diagnostics.Debug.WriteLine("UserData({0}, {1})", userName, validateUserExists.ToString());
            var val = UserDefinition.Parse(userName);

            if (val == null)
            {
                throw new ArgumentException("UserName does not meet expectations");
            }

            if (validateUserExists)
            {
                VoatIdentityUser user = null;
                if (!String.IsNullOrWhiteSpace(userName))
                {
                    using (var repo = VoatUserManager.Create())
                    {
                        user = repo.FindByName(userName);
                    }
                }
                if (user == null)
                {
                    throw new VoatNotFoundException("User doesn't exist");
                }
            }
            _userNameInit = userName;
        }
Пример #2
0
        public async Task TestUserNameAvailability()
        {
            var originalUserName = "******";

            using (var userManager = VoatUserManager.Create())
            {
                var user = new VoatIdentityUser
                {
                    UserName             = originalUserName,
                    RegistrationDateTime = DateTime.UtcNow,
                    LastLoginFromIp      = "127.0.0.1",
                    LastLoginDateTime    = DateTime.UtcNow
                };

                // try to create new user account
                var createResult = await userManager.CreateAsync(user, "topsecretpasswordgoeshere");

                Assert.AreEqual(true, createResult.Succeeded);


                var response = await UserHelper.CanUserNameBeRegistered(userManager, originalUserName, null);

                Assert.AreEqual(false, response);

                response = await UserHelper.CanUserNameBeRegistered(userManager, "iheartfuzzylol", null); //test casing

                Assert.AreEqual(false, response);

                response = await UserHelper.CanUserNameBeRegistered(userManager, "lheartfuzzylol2", null);

                Assert.AreEqual(true, response);

                //Xbox Test
                response = await UserHelper.CanUserNameBeRegistered(userManager, $"xX{originalUserName}Xx", null);

                Assert.AreEqual(true, response);

                Dictionary <string, string> charSwaps = new Dictionary <string, string>();
                charSwaps.Add("i", "l");
                charSwaps.Add("o", "0");
                charSwaps.Add("h", "hahaha");   //just to make sure offset swapping does not break
                charSwaps.Add("heart", "like"); //just to make sure offset swapping does not break

                response = await UserHelper.CanUserNameBeRegistered(userManager, originalUserName, null);

                Assert.AreEqual(false, response);

                response = await UserHelper.CanUserNameBeRegistered(userManager, "iheartfuzzyIoI", charSwaps);

                Assert.AreEqual(false, response);

                response = await UserHelper.CanUserNameBeRegistered(userManager, "lheartfuzzyLOL", charSwaps);

                Assert.AreEqual(false, response);

                response = await UserHelper.CanUserNameBeRegistered(userManager, "lheartFuzzyIOi", charSwaps);

                Assert.AreEqual(false, response);

                response = await UserHelper.CanUserNameBeRegistered(userManager, "lheartFuzzyl0i", charSwaps);

                Assert.AreEqual(false, response);
            }
        }
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (!VoatSettings.Instance.RegistrationEnabled)
            {
                return(View("RegistrationDisabled"));
            }

            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            if (VoatSettings.Instance.ReservedUserNames.Contains(model.UserName.ToLower()))
            {
                ModelState.AddModelError(string.Empty, "The username entered is a reserved name.");
                return(View(model));
            }
            var canBeRegistered = await UserHelper.CanUserNameBeRegistered(null, model.UserName);

            if (!canBeRegistered)
            {
                ModelState.AddModelError(string.Empty, "The username entered is too similar to an existing username. You must modify it in order to register an account.");
                return(View(model));
            }

            if (!Utilities.AccountSecurity.IsPasswordComplex(model.Password, model.UserName, false))
            {
                ModelState.AddModelError(string.Empty, "Your password is not secure. You must use at least one uppercase letter, one lowercase letter, one number and one special character such as ?, ! or .");
                return(View(model));
            }


            try
            {
                // get user IP address
                string clientIpAddress = Request.RemoteAddress();

                // check the number of accounts already in database with this IP address, if number is higher than max conf, refuse registration request
                var accountsWithSameIp = UserManager.Users.Count(x => x.LastLoginFromIp == clientIpAddress);
                if (accountsWithSameIp >= VoatSettings.Instance.MaxAllowedAccountsFromSingleIP)
                {
                    ModelState.AddModelError(string.Empty, "This device can not be used to create a voat account.");
                    return(View(model));
                }

                var user = new VoatIdentityUser
                {
                    UserName             = model.UserName,
                    RegistrationDateTime = Repository.CurrentDate,
                    LastLoginFromIp      = clientIpAddress,
                    LastLoginDateTime    = Repository.CurrentDate
                };

                // try to create new user account
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await _signInManager.SignInAsync(user, isPersistent : false);

                    // redirect new users to Welcome actionresult
                    return(RedirectToAction("Welcome", "Home"));
                }
                AddErrors(result);
            }
            catch (Exception)
            {
                ModelState.AddModelError(string.Empty, "Something bad happened. You broke Voat.");
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }