One of the major design goals of ASP.NET 2.0 was to identify common page developer scenarios and to provide customizable, extensible platform support. One such area in ASP.NET 2.0 is the Membership API, which makes it a breeze to work with user accounts. In the System.Web.Security namespace you'll find the Membership class, which is designed to “validate user credentials and manage user settings.”
One method in the Membership class is GeneratePassword(length, numberOfNonAlphanumeric), which, as it's name implies, generates a random number of length length with at least numberOfNonAlphanumeric characters that are... non-alphanumeric. This method using a cryptographically-strong random number generator to grab a random byte array of the desired length. It then maps these bytes to appropriate alphanumeric and non-alphanumeric characters. Finally, it ensures that at least the numerOfNonAlphanumeric characters has been injected into the random password; if not, it hunts for alphanumeric characters and replaces them with randomly selected non-alphanumeric characters until the threshold is met.
But what if you're still using ASP.NET 1.x and you need to generate a random password? What do you do? Well, why not use Reflector to view the GeneratePassword() method's source code and simply port that back to ASP.NET 1.x code? That's precisely what I did in my most recent 4Guys article, Generating Random Passwords with ASP.NET. (In addition to looking at using GeneratePassword() the article also looks at a “quick and dirty” random password generating technique using GUIDs.)