Example #1
0
        // Randomly generate the desired number of lowercase, uppercase, numeric and special chars
        // Add them to char array
        // Shuffle array randomly
        public static string RandomPassword(AbstractPasswordOptions options)
        {
            int maxLenLowerCaseChars = options.PASSWORD_CHARS_LCASE.Length;
            int maxLenUpperCaseChars = options.PASSWORD_CHARS_UCASE.Length;
            int maxLenNumericChars   = options.PASSWORD_CHARS_NUMERIC.Length;
            int maxLenSpecialChars   = options.PASSWORD_CHARS_SPECIAL.Length;


            int k = 0;

            char[] pwChars = new char[options.TotalPasswordLength];

            using (Captcha.Cryptography.CryptoRandom rnd = options.RandomNumberGenerator)
            {
                for (int i = 0; i < options.NumberOfLowerCaseCharacters; ++i)
                {
                    char c = options.PASSWORD_CHARS_LCASE[rnd.Next(0, maxLenLowerCaseChars)];
                    pwChars[k] = c;
                    k++;
                }


                for (int i = 0; i < options.NumberOfUpperCaseCharacters; ++i)
                {
                    char c = options.PASSWORD_CHARS_UCASE[rnd.Next(0, maxLenUpperCaseChars)];
                    pwChars[k] = c;
                    k++;
                }


                for (int i = 0; i < options.NumberOfNumericCharacters; ++i)
                {
                    char c = options.PASSWORD_CHARS_NUMERIC[rnd.Next(0, maxLenNumericChars)];
                    pwChars[k] = c;
                    k++;
                }


                for (int i = 0; i < options.NumberOfSpecialCharacters; ++i)
                {
                    char c = options.PASSWORD_CHARS_SPECIAL[rnd.Next(0, maxLenSpecialChars)];
                    pwChars[k] = c;
                    k++;
                }

                Shuffle(pwChars, rnd);
            } // End Using rnd

            return(new string(pwChars));
        }// End Function RandomPassword
Example #2
0
        }     // End Sub Shuffle

        public static string CreatePassword(int length)
        {
            System.Text.StringBuilder res = new System.Text.StringBuilder();
            const string valid            = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";

            // System.Random rnd = new System.Random();
            using (Captcha.Cryptography.CryptoRandom rnd = new Captcha.Cryptography.CryptoRandom())
            {
                // cr.Next(10, 100);

                while (0 < length--)
                {
                    res.Append(valid[rnd.Next(valid.Length)]);
                }
            }
            return(res.ToString());
        }