Friday, May 25, 2012

C# - Generate random password string

Use this method to generate random password. You can modify the characters length based on how complex you want the password to be and also decide which characters to use in the random string.



    private static string create_random_password()
    {
        string allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
        char[] chars = new char[13];
        Random rd = new Random();

        for (int i = 0; i <= 13; i++)
        {
            chars[i] = allowedChars[rd.Next(0, allowedChars.Length)];
        }

        return new string(chars);
    }




Enjoy!

No comments:

Post a Comment