Generate Random Password in C#

I recently had to make a method that creates a random generated password in C#.So, I looked at the web for such a function and I found this one. It was really simple and short and just what I was looking for. But, there is always a but, it didn't work. So I modified it a bit, and it now looks like this and it works.

 private static string CreateRandomPassword(int passwordLength)
        string allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789!@$?_-";
        char[] chars = new char[passwordLength];
        Random rd = new Random();

        for (int i = 0; i < passwordLength; i++)
            chars[i] = allowedChars[rd.Next(0, allowedChars.Length)];
        return new string(chars);


No comments:

Post a Comment