Membership::GeneratePassword Method (Int32, Int32)

 

Generates a random password of the specified length.

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

public:
static String^ GeneratePassword(
	int length,
	int numberOfNonAlphanumericCharacters
)

Parameters

length
Type: System::Int32

The number of characters in the generated password. The length must be between 1 and 128 characters.

numberOfNonAlphanumericCharacters
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.

Exception Condition
ArgumentException

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.

System_CAPS_noteNote

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.

System_CAPS_security Security 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
Available since 2.0
Return to top
Show: