Share via


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

定義

更新成員資格資料存放區中成員資格使用者的密碼問答。

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

參數

password
String

成員資格使用者的目前密碼。

newPasswordQuestion
String

成員資格使用者的新密碼問題值。

newPasswordAnswer
String

成員資格使用者的新密碼解答值。

傳回

如果更新成功,則為 true;否則為 false

例外狀況

password 為空字串。

-或-

newPasswordQuestion 為空字串。

-或-

newPasswordAnswer 為空字串。

passwordnull

這個方法無法使用。 如果應用程式以 .NET Framework 4 用戶端設定檔為目標,就會發生這種情況。 若要避免這個例外狀況,請覆寫方法,或將應用程式變更為以完整版本的 .NET Framework 為目標。

範例

下列程式碼範例會更新目前登入使用者的密碼問題和解答。

重要

此範例包含一個文字方塊,可接受使用者輸入,這是潛在的安全性威脅。 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。


<%@ 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
    {
      MembershipUser u = Membership.GetUser(User.Identity.Name);
      Boolean result = u.ChangePasswordQuestionAndAnswer(PasswordTextbox.Text,
                                        QuestionTextbox.Text,
                                        AnswerTextbox.Text);

      if (result)
        Msg.Text = "Password Question and Answer changed.";
      else
        Msg.Text = "Password Question and Answer change failed.";
    }
    catch (Exception e)
    {
      Msg.Text = "Change failed. Please re-enter 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(ByVal sender As Object, ByVal args As EventArgs)
    Try
      Dim u As MembershipUser = Membership.GetUser(User.Identity.Name)
      Dim result As Boolean
      result = u.ChangePasswordQuestionAndAnswer(PasswordTextbox.Text, _
                                      QuestionTextbox.Text, _
                                      AnswerTextbox.Text)
  
      If (result = True) Then
        Msg.Text = "Password Question and Answer changed."
      Else
        Msg.Text = "Password Question and Answer change failed."
      End If
      
    Catch e As Exception
      Msg.Text = "Change failed. Please re-enter 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>

備註

ChangePasswordQuestionAndAnswerChangePasswordQuestionAndAnswer呼叫 屬性所參考 ProviderName 之成員資格提供者的 方法,以更新成員資格資料存放區中成員資格使用者的密碼問題和答案。

成員資格提供者可能會限制密碼問題和答案的大小。 如需大小限制,請參閱成員資格提供者的檔。

適用於

另請參閱