This documentation is archived and is not being maintained.

Membership::GeneratePassword Method

Generates a random password of the specified length.

Namespace:  System.Web.Security
Assembly:  System.Web (in System.Web.dll)

static String^ GeneratePassword(
	int length, 
	int numberOfNonAlphanumericCharacters


Type: System::Int32
The number of characters in the generated password. The length must be between 1 and 128 characters.
Type: System::Int32
The minimum number of non-alphanumeric characters (such as @, #, !, %, &, and so on) in the generated password.

Return Value

Type: System::String
A random password of the specified length.


length is less than 1 or greater than 128 -or-

numberOfNonAlphanumericCharacters is less than 0 or greater than length.

The GeneratePassword method is used to generate a random password and is most commonly used by the ResetPassword method implemented by a membership provider to reset the password for a user to a new, temporary password.

The generated password only contains alphanumeric characters and the following punctuation marks: !@#$%^&*()_-+=[{]};:<>|./?. No hidden or non-printable control characters are included in the generated password.


The random password created by the GeneratePassword method is not guaranteed to pass the regular expression in the PasswordStrengthRegularExpression property. However, the random password will meet the criteria established by the MinRequiredPasswordLength property and the numberOfNonAlphanumericCharacters parameter.

The following code example creates a new membership user and uses the GeneratePassword method to initialize the password for the new user to a random value, which is displayed to the user.

Security noteSecurity Note

This example contains a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.