예제 #1
0
        public IHttpActionResult Get(string word)
        {
            var validation = new PasswordValidationService();
            var response   = validation.MeasurePasswordStrength(word);

            return(Ok(response));
        }
        public ActionResult RegisterUser(LoginModel loginModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(loginModel));
            }


            var validationService = new PasswordValidationService();

            if (loginModel.Password != loginModel.ConfirmPassword)
            {
                ModelState.AddModelError("Password", "As senhas não conferem");
                return(View(loginModel));
            }

            if (validationService.MeasurePasswordStrength(loginModel.Password) < 100)
            {
                ModelState.AddModelError("Password", "Senha não considerada forte. Por favor, coloque uma nova senha");
                return(View(loginModel));
            }

            loginModel.Password        = _desEncryptor.Encrypt(loginModel.Password);
            loginModel.ConfirmPassword = _desEncryptor.Encrypt(loginModel.ConfirmPassword);

            _repository.Save(loginModel);

            return(RedirectToAction("Index", "Home"));
        }
예제 #3
0
        public override bool IsValid(object value)
        {
            const double maxPasswordStrength = 100;

            var validationService = new PasswordValidationService();

            return(validationService.MeasurePasswordStrength((string)value) == maxPasswordStrength);
        }
예제 #4
0
        public string Generate(int length, ValidationType validationType)
        {
            var passwordValidation = new PasswordValidationService();

            var wordChar = GenerateChars(length, validationType);
            var word     = new String(wordChar);
            var measuredPasswordStrength = passwordValidation.MeasurePasswordStrength(word);

            if (validationType == ValidationType.Strong)
            {
                while (measuredPasswordStrength != 100)
                {
                    wordChar = GenerateChars(length, validationType);
                    word     = new String(wordChar);
                    measuredPasswordStrength = passwordValidation.MeasurePasswordStrength(word);
                }
            }
            else if (validationType == ValidationType.Medium)
            {
                while (measuredPasswordStrength != 50)
                {
                    wordChar = GenerateChars(length, validationType);
                    word     = new String(wordChar);
                    measuredPasswordStrength = passwordValidation.MeasurePasswordStrength(word);
                }
            }
            else if (validationType == ValidationType.Weak)
            {
                while (measuredPasswordStrength != 20)
                {
                    wordChar = GenerateChars(length, validationType);
                    word     = new String(wordChar);
                    measuredPasswordStrength = passwordValidation.MeasurePasswordStrength(word);
                }
            }

            return(word);
        }
        public void WhenAWeakPasswordIsRequestedAWeakPasswordShouldBeCreated()
        {
            var passwordGenerated = _passwordGenerator.Generate(4, ValidationType.Weak);

            Assert.AreEqual(_passwordValidationService.MeasurePasswordStrength(passwordGenerated), 20);
        }