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 要素が含まれていないかどうかが検証されます。 詳細については、「スクリプトによる攻略の概要」を参照してください。


<%@ 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>

注釈

ChangePasswordQuestionAndAnswer は、 ChangePasswordQuestionAndAnswer プロパティによって参照されるメンバーシップ プロバイダーの メソッドを ProviderName 呼び出して、メンバーシップ データ ストアのメンバーシップ ユーザーのパスワードの質問と回答を更新します。

メンバーシップ プロバイダーには、パスワードの質問と回答のサイズに制限がある場合があります。 サイズの制限については、メンバーシップ プロバイダーのドキュメントを参照してください。

適用対象

こちらもご覧ください