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

定义

更新 SQL Server 成员资格数据库中用户的密码提示问题和答案。Updates the password question and answer for a user in the SQL Server membership database.

public:
 override bool ChangePasswordQuestionAndAnswer(System::String ^ username, System::String ^ password, System::String ^ newPasswordQuestion, System::String ^ newPasswordAnswer);
public override bool ChangePasswordQuestionAndAnswer (string username, string password, string newPasswordQuestion, string newPasswordAnswer);
override this.ChangePasswordQuestionAndAnswer : string * string * string * string -> bool
Public Overrides Function ChangePasswordQuestionAndAnswer (username As String, password As String, newPasswordQuestion As String, newPasswordAnswer As String) As Boolean

参数

username
String

要为其更改密码提示问题和答案的用户。The user to change the password question and answer for.

password
String

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

newPasswordQuestion
String

指定的用户的新密码提示问题。The new password question for the specified user.

newPasswordAnswer
String

指定的用户的新密码提示问题答案。The new password answer for the specified user.

返回

如果更新操作成功,则为 true;否则为 falsetrue if the update was successful; otherwise, false. 如果 password 错误、用户被锁定或数据库中不存在该用户,还将返回 false 值。A value of false is also returned if the password is incorrect, 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- password 为空字符串,或者长度超过 128 个字符。password is an empty string or is longer than 128 characters.

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

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

-or- newPasswordAnswer 的编码版本长度 128 个字符。The encoded version of newPasswordAnswer is longer than 128 characters.

usernamenullusername is null.

-or- passwordnullpassword is null.

-or- newPasswordQuestionnull,而 RequiresQuestionAndAnswertruenewPasswordQuestion is null and RequiresQuestionAndAnswer is true.

-or- newPasswordAnswernull,而 RequiresQuestionAndAnswertruenewPasswordAnswer is null and RequiresQuestionAndAnswer is true.

更改数据库中的密码提示问题和答案时出错。An error occurred when changing the password question and answer in the database.

示例

下面的代码示例将更新用户的密码提示问题和答案。The following code example updates the password question and answer for a 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 ChangePasswordQuestion_OnClick(object sender, EventArgs args)
{
  try
  {
    if (Membership.Provider.ChangePasswordQuestionAndAnswer(User.Identity.Name,
                                                            PasswordTextbox.Text, 
                                                            QuestionTextbox.Text, 
                                                            AnswerTextbox.Text))
      Msg.Text = "Password question and answer changed.";
    else
      Msg.Text = "Change failed. Please reenter your values and try again.";
  }
  catch (System.Configuration.Provider.ProviderException e)
  {
    Msg.Text = "Change failed. Please reenter your values and try again.";
  }
}

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

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

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

  <table cellpadding="3" border="0">
    <tr>
      <td>Password:</td>
      <td><asp:Textbox id="PasswordTextbox" runat="server" TextMode="Password" /></td>
      <td><asp:RequiredFieldValidator id="OldPasswordRequiredValidator" runat="server"
                                    ControlToValidate="PasswordTextbox" ForeColor="red"
                                    Display="Static" ErrorMessage="Required" /></td>
    </tr>
    <tr>
      <td>New Password Question:</td>
      <td><asp:Textbox id="QuestionTextbox" MaxLength="256" Columns="60" runat="server" /></td>
      <td><asp:RequiredFieldValidator id="QuestionRequiredValidator" runat="server"
                                    ControlToValidate="QuestionTextbox" ForeColor="red"
                                    Display="Static" ErrorMessage="Required" /></td>
    </tr>
    <tr>
      <td>New Password Answer:</td>
      <td><asp:Textbox id="AnswerTextbox" MaxLength="128" Columns="60" runat="server" /></td>
      <td><asp:RequiredFieldValidator id="AnswerRequiredValidator" runat="server"
                                    ControlToValidate="AnswerTextbox" ForeColor="red"
                                    Display="Static" ErrorMessage="Required" /></td>
    </tr>
    <tr>
      <td></td>
      <td><asp:Button id="ChangePasswordQuestionButton" 
                      Text="Change Password Question and Answer" 
                      OnClick="ChangePasswordQuestion_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 ChangePasswordQuestion_OnClick(sender As Object, args As EventArgs)

  Try
    If Membership.Provider.ChangePasswordQuestionAndAnswer(User.Identity.Name, _
                                                          PasswordTextbox.Text, _ 
                                                          QuestionTextbox.Text, _
                                                          AnswerTextbox.Text) Then
      Msg.Text = "Password question and answer changed."
    Else
      Msg.Text = "Change failed. Please reenter your values and try again."
    End If
  Catch e As System.Configuration.Provider.ProviderException
    Msg.Text = "Change failed. Please reenter your values and try again."
  End Try

End Sub

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

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

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

  <table cellpadding="3" border="0">
    <tr>
      <td>Password:</td>
      <td><asp:Textbox id="PasswordTextbox" runat="server" TextMode="Password" /></td>
      <td><asp:RequiredFieldValidator id="OldPasswordRequiredValidator" runat="server"
                                    ControlToValidate="PasswordTextbox" ForeColor="red"
                                    Display="Static" ErrorMessage="Required" /></td>
    </tr>
    <tr>
      <td>New Password Question:</td>
      <td><asp:Textbox id="QuestionTextbox" MaxLength="256" Columns="60" runat="server" /></td>
      <td><asp:RequiredFieldValidator id="QuestionRequiredValidator" runat="server"
                                    ControlToValidate="QuestionTextbox" ForeColor="red"
                                    Display="Static" ErrorMessage="Required" /></td>
    </tr>
    <tr>
      <td>New Password Answer:</td>
      <td><asp:Textbox id="AnswerTextbox" MaxLength="128" Columns="60" runat="server" /></td>
      <td><asp:RequiredFieldValidator id="AnswerRequiredValidator" runat="server"
                                    ControlToValidate="AnswerTextbox" ForeColor="red"
                                    Display="Static" ErrorMessage="Required" /></td>
    </tr>
    <tr>
      <td></td>
      <td><asp:Button id="ChangePasswordQuestionButton" 
                      Text="Change Password Question and Answer" 
                      OnClick="ChangePasswordQuestion_OnClick" 
                      runat="server" /></td>
    </tr>
  </table>
</form>

</body>
</html>

注解

此方法由MembershipUser类调用,以更新在 ASP.NET 应用程序的配置文件(web.config)中指定的 SQL Server 数据库中的用户的密码提示问题和答案。This method is called by the MembershipUser class to update the password question and answer for a user in the SQL Server database that is specified in the ASP.NET application's configuration file (Web.config). 密码答案是使用PasswordFormat属性中指定的格式进行加密的。The password answer is encrypted using the format that is specified in the PasswordFormat property.

在检索或重置用户密码时,需要密码提示问题和答案可以提供额外的安全层。Requiring a password question and answer provides an additional layer of security when retrieving or resetting a user's password. 创建用户名时,用户可以提供稍后可用于检索或重置忘记密码的问题和答案。When creating a user name, a user can supply a question and answer that can later be used to retrieve or reset a forgotten password. ChangePasswordQuestionAndAnswer方法为成员资格用户更新密码提示问题和答案。The ChangePasswordQuestionAndAnswer method updates the password question and answer for a membership user.

如果为ChangePasswordQuestionAndAnswer方法提供了错误密码,则跟踪无效密码尝试的内部计数器会递增1。If an incorrect password is supplied to the ChangePasswordQuestionAndAnswer method, the internal counters that track 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 calling 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.

密码提示问题的最大长度为256个字符。The maximum length for the password question is 256 characters. 密码答案的最大长度为128个字符。The maximum length for the password answer is 128 characters.

有关详细信息,请参阅RequiresQuestionAndAnswerResetPasswordGetPasswordFor more information, see RequiresQuestionAndAnswer, ResetPassword, and GetPassword.

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

适用于

另请参阅