[ news_security_news ] Generate Random Password In C#
Mads Kristensen Contributing Writer
2006-10-16
Insider Reports RSS Feed
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);
}
It's that simple.
Tag: C#, password
Add to Del.icio.us | Digg | Yahoo! My Web | Furl
Get all the updates in RSS:
About the Author:
Mads Kristensen currently works as a Senior Developer at Traceworks located
in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in
2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and
web services in his daily work as well. A true .NET developer with great passion for the simple solution.
http://www.madskristensen.dk/
More news_security_news Articles
Insider Reports RSS Feed
|
|