SqlMembershipProvider.ChangePasswordQuestionAndAnswer 方法

定义

更新 SQL Server 成员资格数据库中用户的密码提示问题和答案。

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

要为其更改密码提示问题和答案的用户。

password
String

指定的用户的密码。

newPasswordQuestion
String

指定的用户的新密码提示问题。

newPasswordAnswer
String

指定的用户的新密码提示问题答案。

返回

Boolean

如果更新操作成功,则为 true;否则为 false。 如果 password 错误、用户被锁定或数据库中不存在该用户,还将返回 false 值。

例外

username 为空字符串 ("")、包含一个逗号或长度超过 256 个字符。

  • 或 - password 为空字符串,或者长度超过 128 个字符。

  • 或 - newPasswordQuestion 为空字符串,或者长度超过 256 个字符。

  • 或 - newPasswordAnswer 为空字符串,或者长度超过 128 个字符。

  • 或 - newPasswordAnswer 的编码版本长度 128 个字符。

usernamenull

更改数据库中的密码提示问题和答案时出错。

示例

下面的代码示例更新用户的密码问题和答案。

备注

此示例使用 Provider 类的属性 Membership 调用 SqlMembershipProvider 指定为 defaultProvider Web.config文件中的指定属性。 如果需要以类型 SqlMembershipProvider的形式访问默认提供程序,则可以强制转换 Provider 类的属性 Membership 。 若要以特定提供程序类型的形式访问其他配置的 Providers 提供程序,可以使用类的属性 Membership 访问它们,并将其强制转换为特定提供程序类型。

<%@ 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数据库中的用户的密码问题和答案。 密码答案使用属性中指定的 PasswordFormat 格式进行加密。

检索或重置用户密码时,要求密码问题和答案提供额外的安全层。 创建用户名时,用户可以提供一个问题和答案,供以后用于检索或重置忘记的密码。 该方法 ChangePasswordQuestionAndAnswer 更新成员身份用户的密码问题和答案。

如果向 ChangePasswordQuestionAndAnswer 该方法提供不正确的密码,跟踪无效密码尝试的内部计数器将递增一个。 这可能会导致用户被锁定,并且无法登录,直到通过调用 UnlockUser 该方法清除锁定状态。 如果提供了正确的密码,并且用户当前未锁定,则跟踪无效密码和密码应答尝试的内部计数器将重置为零。 有关详细信息,请参阅 MaxInvalidPasswordAttemptsPasswordAttemptWindow 属性。

密码问题的最大长度为 256 个字符。 密码答案的最大长度为 128 个字符。

有关详细信息,请参阅RequiresQuestionAndAnswerResetPasswordGetPassword

从所有参数值中剪裁前导空格和尾随空格。

适用于

另请参阅