SqlMembershipProvider.GeneratePassword 方法


生成长度至少为 14 个字符的随机密码。Generates a random password that is at least 14 characters long.

 virtual System::String ^ GeneratePassword();
public virtual string GeneratePassword ();
abstract member GeneratePassword : unit -> string
override this.GeneratePassword : unit -> string
Public Overridable Function GeneratePassword () As String



长度至少为 14 个字符的随机密码。A random password that is at least 14 characters long.


下面的代码示例生成一个随机10个字符的密码。The following code example generates a random 10-character password.

SqlMembershipProvider p = (SqlMembershipProvider)Membership.Provider;
string newPassword = p.GeneratePassword();
Dim p As SqlMembershipProvider = CType(Membership.Provider, SqlMembershipProvider)
Dim newPassword As String = p.GeneratePassword()


Membership类提供一个 GeneratePassword 方法,该方法将生成指定大小至少为指定数量的非字母字符的密码。The Membership class provides a GeneratePassword method, which generates a password of a specified size with at least the specified number of non-alphabetic characters. GeneratePassword的方法 SqlMembershipProvider 调用 GeneratePassword 类的方法 Membership 来检索长度至少为14个字符的随机密码。The GeneratePassword method of the SqlMembershipProvider calls the GeneratePassword method of the Membership class to retrieve a random password that is at least 14 characters long. 如果 MinRequiredPasswordLength 属性大于14,则返回的密码将是属性中指定的长度 MinRequiredPasswordLengthIf the MinRequiredPasswordLength property is greater than 14, the password returned will be the length specified in the MinRequiredPasswordLength property.

您可以 GeneratePassword 通过 SqlMembershipProvider 直接从类的属性中引用类来专门调用方法 Provider MembershipYou can specifically call the GeneratePassword method by referencing the SqlMembershipProvider class directly from the Provider property of the Membership class.

生成的密码只包含字母数字字符和以下标点符号: !@#$%^&*()_-+=[{]};:<>|./?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.


不保证通过方法创建的随机密码 GeneratePassword 在属性中传递正则表达式 PasswordStrengthRegularExpressionThe random password created by the GeneratePassword method is not guaranteed to pass the regular expression in the PasswordStrengthRegularExpression property. 但是,随机密码会满足和属性所建立的条件 MinRequiredPasswordLength MinRequiredNonAlphanumericCharactersHowever, the random password will meet the criteria established by the MinRequiredPasswordLength and MinRequiredNonAlphanumericCharacters properties.