Пример #1
0
        public void Create()
        {
            // Configure validation logic for passwords
            PasswordValidator passwordValidator = new PasswordValidator
            {
                RequiredLength          = 6,
                RequireNonLetterOrDigit = true,
                RequireDigit            = true,
                RequireLowercase        = true,
                RequireUppercase        = true,
            };

            Microsoft.AspNet.Identity.PasswordValidator passwordValidator1 = new Microsoft.AspNet.Identity.PasswordValidator
            {
                RequiredLength          = 6,
                RequireNonLetterOrDigit = true
            };
            passwordValidator1.RequireDigit     = true;
            passwordValidator1.RequireLowercase = true;
            passwordValidator1.RequireUppercase = true;
            passwordValidator1.RequiredLength   = 7;

            PasswordValidator passwordValidator2 = new PasswordValidator();

            passwordValidator2.RequiredLength          = 6;
            passwordValidator2.RequireNonLetterOrDigit = true;
            passwordValidator2.RequireDigit            = true;
            passwordValidator2.RequireLowercase        = true;
            passwordValidator2.RequireUppercase        = true;
        }
Пример #2
0
        public static string GeneratePassword(Microsoft.AspNet.Identity.PasswordValidator validator)
        {
            if (validator == null)
            {
                throw new ArgumentException("Missing parameter: ", nameof(validator));
            }

            bool requireNonLetterOrDigit = validator.RequireNonLetterOrDigit;
            bool requireDigit            = validator.RequireDigit;
            bool requireLowercase        = validator.RequireLowercase;
            bool requireUppercase        = validator.RequireUppercase;

            string randomPassword = string.Empty;

            int passwordLength = validator.RequiredLength;

            Random random = new Random();

            while (randomPassword.Length != passwordLength)
            {
                int randomNumber = random.Next(48, 122);  // >= 48 && < 122
                if (randomNumber == 95 || randomNumber == 96)
                {
                    continue;                                            // != 95, 96 _'
                }
                char c = Convert.ToChar(randomNumber);

                if (requireDigit)
                {
                    if (char.IsDigit(c))
                    {
                        requireDigit = false;
                    }
                }

                if (requireLowercase)
                {
                    if (char.IsLower(c))
                    {
                        requireLowercase = false;
                    }
                }

                if (requireUppercase)
                {
                    if (char.IsUpper(c))
                    {
                        requireUppercase = false;
                    }
                }

                if (requireNonLetterOrDigit)
                {
                    if (!char.IsLetterOrDigit(c))
                    {
                        requireNonLetterOrDigit = false;
                    }
                }

                randomPassword += c;
            }

            Action <int, int> jokerCharInsert =
                (fromAscii, toAscii) =>
            {
                var pos = random.Next(0, randomPassword.Length - 1);
                var chr = Convert.ToChar(random.Next(fromAscii, toAscii)).ToString();
                randomPassword = randomPassword.Insert(pos, chr);
            };

            if (requireDigit)
            {
                jokerCharInsert(48, 58);
            }

            if (requireLowercase)
            {
                jokerCharInsert(97, 123);
            }

            if (requireUppercase)
            {
                jokerCharInsert(65, 91);
            }

            if (requireNonLetterOrDigit)
            {
                jokerCharInsert(33, 48);
            }

            return(randomPassword);
        }