How to generate a random password with .NET

I have been working with ASP.NET MVC4 and needed the ability to generate a random password for a personal website I have been building. The System.Web.Security namespace makes it very easy to accomplish this through one line of code.

string randomPassword = Membership.GeneratePassword(8, 2);

The GeneratePassword method takes in two parameters. The first is the length of the password you wish to generate and the second is the number of non-alphanumeric characters to include in the password. Here is an example of some randomly generated passwords using this method.

cWkX-2+G
*}MPeRZy
cV&^f$-C

Enjoy!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s