SqlMembershipProvider.ChangePassword(String, String, String) 方法

定义

修改用户密码。Modifies a user's password.

public:
 override bool ChangePassword(System::String ^ username, System::String ^ oldPassword, System::String ^ newPassword);
public override bool ChangePassword (string username, string oldPassword, string newPassword);
override this.ChangePassword : string * string * string -> bool
Public Overrides Function ChangePassword (username As String, oldPassword As String, newPassword As String) As Boolean

参数

username
String

要为其更新密码的用户。The user to update the password for.

oldPassword
String

指定的用户的当前密码。The current password for the specified user.

newPassword
String

指定的用户的新密码。The new password for the specified user.

返回

如果密码更新成功,则为 truetrue if the password was updated successfully. 如果提供的旧密码无效、用户被锁定或数据库中不存在该用户,则返回 falsefalse if the supplied old password is invalid, the user is locked out, or the user does not exist in the database.

异常

username 为空字符串 ("")、包含一个逗号或长度超过 256 个字符。username is an empty string (""), contains a comma, or is longer than 256 characters.

-or- oldPassword 为空字符串,或者长度超过 128 个字符。oldPassword is an empty string or longer than 128 characters.

-or- newPassword 为空字符串,或者长度超过 128 个字符。newPassword is an empty string or longer than 128 characters.

- 或 --or- 编码版本的 newPassword 的长度超过 128 个字符。The encoded version of newPassword is greater than 128 characters.

-or- 更改密码操作被 ValidatingPassword 事件的订户取消,并且 FailureInformation 属性为 nullThe change-password action was canceled by a subscriber to the ValidatingPassword event, and the FailureInformation property was null.

- 或 --or- newPassword 的长度小于 MinRequiredPasswordLength 属性中指定的最小长度。The length of newPassword is less than the minimum length specified in the MinRequiredPasswordLength property.

-or- newPassword 中非字母字符数量少于 MinRequiredNonAlphanumericCharacters 属性中指定的所需非字母字符数量。The number of non-alphabetic characters in newPassword is less than the required number of non-alphabetic characters specified in the MinRequiredNonAlphanumericCharacters property.

- 或 --or- newPassword 未通过 PasswordStrengthRegularExpression 属性中定义的正则表达式的验证。newPassword does not pass the regular expression defined in the PasswordStrengthRegularExpression property.

usernamenullusername is null.

-or- oldPasswordnulloldPassword is null.

- 或 --or- newPasswordnullnewPassword is null.

在数据库中找不到 usernameusername was not found in the database.

设置数据库新密码值时出现错误。An error occurred while setting the new password value at the database.

发生了未经处理的异常。An unhandled exception occurred.

示例

下面的代码示例修改指定用户的密码。The following code example modifies the password for the specified user.

备注

Provider示例使用Membership类的属性,以defaultProvider在 web.config 文件SqlMembershipProvider中调用指定的。This example uses the Provider property of the Membership class to call the SqlMembershipProvider specified as the defaultProvider in the Web.config file. 如果需要以类型SqlMembershipProvider访问默认提供程序,则可以Provider强制转换Membership类的属性。If you need to access the default provider as the type SqlMembershipProvider, you can cast the Provider property of the Membership class. 若要以特定的提供程序类型访问其他已配置的提供程序,可以使用Providers Membership类的属性访问这些提供程序,并将其转换为特定的提供程序类型。To access other configured providers as a specific provider type, you can access them by their configured name with the Providers property of the Membership class and cast them as the specific provider type.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Security" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

public void ChangePassword_OnClick(object sender, EventArgs args)
{
  try
  {
    // Update the password.

    if (Membership.Provider.ChangePassword(User.Identity.Name, OldPasswordTextbox.Text, PasswordTextbox.Text))
    {
      Msg.Text = "Password changed.";
      return;
    }
  }
  catch
  {
  }

  Msg.Text = "Password change failed. Please re-enter your values and try again.";
}


</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Change Password</title>
</head>
<body>

<form id="form1" runat="server">
  <h3>Change Password for <%=User.Identity.Name%></h3>

  <asp:Label id="Msg" ForeColor="maroon" runat="server" />

  <table cellpadding="3" border="0">
    <tr>
      <td>Old Password:</td>
      <td><asp:Textbox id="OldPasswordTextbox" runat="server" TextMode="Password" /></td>
      <td><asp:RequiredFieldValidator id="OldPasswordRequiredValidator" runat="server"
                                      ControlToValidate="OldPasswordTextbox" ForeColor="red"
                                      Display="Static" ErrorMessage="Required" /></td>
    </tr>
    <tr>
      <td>Password:</td>
      <td><asp:Textbox id="PasswordTextbox" runat="server" TextMode="Password" /></td>
      <td><asp:RequiredFieldValidator id="PasswordRequiredValidator" runat="server"
                                      ControlToValidate="PasswordTextbox" ForeColor="red"
                                      Display="Static" ErrorMessage="Required" /></td>
    </tr>
    <tr>
      <td>Confirm Password:</td>
      <td><asp:Textbox id="PasswordConfirmTextbox" runat="server" TextMode="Password" /></td>
      <td><asp:RequiredFieldValidator id="PasswordConfirmRequiredValidator" runat="server"
                                      ControlToValidate="PasswordConfirmTextbox" ForeColor="red"
                                      Display="Static" 
                                      ErrorMessage="Required" />
          <asp:CompareValidator id="PasswordConfirmCompareValidator" runat="server"
                                      ControlToValidate="PasswordConfirmTextbox" ForeColor="red"
                                      Display="Static" ControlToCompare="PasswordTextBox"
                                      ErrorMessage="Confirm password must match password." />
      </td>
    </tr>
    <tr>
      <td></td>
      <td><asp:Button id="ChangePasswordButton" Text="Change Password" 
                      OnClick="ChangePassword_OnClick" runat="server" /></td>
    </tr>
  </table>
</form>

</body>
</html>
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.Security" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

Public Sub ChangePassword_OnClick(sender As Object, args As EventArgs)
  Try
    ' Update the password.

    If Membership.Provider.ChangePassword(User.Identity.Name, _
                                          OldPasswordTextbox.Text, _
                                          PasswordTextbox.Text) Then 
      Msg.Text = "Password changed."
      Return
    End If
  Catch
  End Try

  Msg.Text = "Password change failed. Please re-enter your values and try again."
End Sub

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Change Password</title>
</head>
<body>

<form id="form1" runat="server">
  <h3>Change Password for <%=User.Identity.Name%></h3>

  <asp:Label id="Msg" ForeColor="maroon" runat="server" />

  <table cellpadding="3" border="0">
    <tr>
      <td>Old Password:</td>
      <td><asp:Textbox id="OldPasswordTextbox" runat="server" TextMode="Password" /></td>
      <td><asp:RequiredFieldValidator id="OldPasswordRequiredValidator" runat="server"
                                      ControlToValidate="OldPasswordTextbox" ForeColor="red"
                                      Display="Static" ErrorMessage="Required" /></td>
    </tr>
    <tr>
      <td>Password:</td>
      <td><asp:Textbox id="PasswordTextbox" runat="server" TextMode="Password" /></td>
      <td><asp:RequiredFieldValidator id="PasswordRequiredValidator" runat="server"
                                      ControlToValidate="PasswordTextbox" ForeColor="red"
                                      Display="Static" ErrorMessage="Required" /></td>
    </tr>
    <tr>
      <td>Confirm Password:</td>
      <td><asp:Textbox id="PasswordConfirmTextbox" runat="server" TextMode="Password" /></td>
      <td><asp:RequiredFieldValidator id="PasswordConfirmRequiredValidator" runat="server"
                                      ControlToValidate="PasswordConfirmTextbox" ForeColor="red"
                                      Display="Static" 
                                      ErrorMessage="Required" />
          <asp:CompareValidator id="PasswordConfirmCompareValidator" runat="server"
                                      ControlToValidate="PasswordConfirmTextbox" ForeColor="red"
                                      Display="Static" ControlToCompare="PasswordTextBox"
                                      ErrorMessage="Confirm password must match password." />
      </td>
    </tr>
    <tr>
      <td></td>
      <td><asp:Button id="ChangePasswordButton" Text="Change Password" 
                      OnClick="ChangePassword_OnClick" runat="server" /></td>
    </tr>
  </table>
</form>

</body>
</html>

注解

此方法由Membership类调用,以更新 ASP.NET 应用程序的配置文件(web.config)中指定的 SQL Server 数据库中用户的密码。This method is called by the Membership class to update the password for a user in the SQL Server database specified in the ASP.NET application's configuration file (Web.config).

最大密码长度为128个字符。The maximum password length is 128 characters.

如果为ChangePassword方法提供了错误密码,则跟踪无效密码尝试的内部计数器会递增1。If an incorrect password is supplied to the ChangePassword method, the internal counter that tracks invalid password attempts is incremented by one. 这可能会导致用户被锁定,并且在通过调用UnlockUser方法清除锁定状态之前无法登录。This can result in the user being locked out and unable to log on until the lock status is cleared by a call to the UnlockUser method. 如果提供了正确的密码并且用户当前未锁定,则跟踪无效密码和密码答案尝试的内部计数器将重置为零。If the correct password is supplied and the user is not currently locked out, then the internal counters that track invalid password and password-answer attempts are reset to zero. 有关详细信息,请参阅MaxInvalidPasswordAttemptsPasswordAttemptWindow属性。For more information, see the MaxInvalidPasswordAttempts and PasswordAttemptWindow properties.

ChangePassword通过先SqlMembershipProvider 通过Provider类的属性获取对实例的引用,可以直接调用方法。 MembershipYou can call the ChangePassword method directly by first obtaining a reference to the SqlMembershipProvider instance through the Provider property of the Membership class. 属性公开应用程序的 web.config 文件中指定的defaultProviderProviderThe Provider property exposes the defaultProvider specified in the Web.config file for the application. 配置的提供程序,这些提供程序不是使用Providers属性引用的默认提供程序。Configured providers that are not the default provider referenced using the Providers property.

还可以使用ChangePassword方法更改用户密码。You can also change user passwords by using the ChangePassword method.

将从所有参数值中去除前导空格和尾随空格。Leading and trailing spaces are trimmed from all parameter values.

适用于

另请参阅