Generate a Random Password Using C#

by Edward 01 November 2010 19:15

Every website that holds important or sensitive data, should have some type of password policy. In my example below you can generate your own random password, that will be secure and not easy to read. For example when a new user is created and you can't think of a password, or you need the password to be as random as possible. This password generator method will generate secure, random password examples for you to use.

Select the password length, and the type(eg: if you do not want symbols in your password), and your password will be generated for you.

This is how you would call the password generator method:

            Debug.WriteLine("Type 1: " + GenerateRandomPassword(20, 1));
            Debug.WriteLine("Type 2: " + GenerateRandomPassword(20, 2));
            Debug.WriteLine("Type 3: " + GenerateRandomPassword(20, 3));
            Debug.WriteLine("Type 4: " + GenerateRandomPassword(20, 4));

The method that generates the password(this is for example purposes):

        /// <summary>
        /// Generates the random password.
        /// </summary>
        /// <param name="passwordLength">Length of the password.</param>
        /// <param name="type">The type of password needed.</param>
        /// <returns></returns>
        private static string GenerateRandomPassword(int passwordLength, int type)
        {
            const string allowedChars = "abcdefghijkmnopqrstuvwxyz";
            const string allowedCharsWithCaps = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ";
            const string allowedCharsWithCapsAndNumbers = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
            const string allowedCharsWithCapsAndNumbersAndSymbols = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789!@$?_-";

            char[] chars = new char[passwordLength];
            Random rd = new Random();
            string passwordCombinations;

            switch (type)
            {
                case 1:
                    passwordCombinations = allowedChars;
                    break;
                case 2:
                    passwordCombinations = allowedCharsWithCaps;
                    break;
                case 3:
                    passwordCombinations = allowedCharsWithCapsAndNumbers;
                    break;
                case 4:
                    passwordCombinations = allowedCharsWithCapsAndNumbersAndSymbols;
                    break;
                default:
                    passwordCombinations = allowedChars;
                    break;
            }

            for (int i = 0; i < passwordLength; i++)
            {
                chars[i] = passwordCombinations[rd.Next(0, passwordCombinations.Length - 1)];
            }

            return new string(chars);
        }


Quick Tip: Including numbers and symbols in a mixed case password will generally create a more secure password, which would be exponentially harder to recover using a brute force password discovery method. Also remember that this code sample is for demostration only, to give you a starting point on creating passwords.

Tags: , ,

ASP.NET | Other

Comments are closed

About DasCode.Net

I'm a ASP.NET web developer and code enthusiast. Blogging about everything .Net related.

Code... that's .net

Month List