Easily generate passwords in C# using the Membership.GeneratePassword method
Posted: (EET/GMT+2)
When working with login functionality in your .NET application, you might run into the need to quickly generate a new password that is of good quality. Writing such a generator routine isn't overly difficult, but did you know that the .NET library contains such a routine ready made?
Take a look at the Membership.GeneratePassword method in the System.Web.Security namespace. Using this static method is very easy: just pass in the number of characters you want in the generated password. Additionally, you can specify how many special characters (such as %, # or /) you want in the password.
For example, making this call:
string pwd = System.Web.Security.Membership.GeneratePassword(10,2);
...could generate this kind of password:
n(IXr2Fbz_
...which is already a pretty good password.
To get access to the Membership class, remember to add a reference to "System.Web.dll" in your project.
Hope this helps!