MembershipUser.ChangePasswordQuestionAndAnswer(String, String, String) Metodo

Definizione

Aggiorna la domanda e la risposta segreta per l'utente di appartenenza nell'archivio dati di appartenenza.

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

Parametri

password
String

Password corrente per l'utente di appartenenza.

newPasswordQuestion
String

Nuovo valore della domanda segreta per l'utente di appartenenza.

newPasswordAnswer
String

Nuovo valore della risposta segreta per l'utente di appartenenza.

Restituisce

true se l'aggiornamento è stato eseguito correttamente. In caso contrario, false.

Eccezioni

Il parametro password è una stringa vuota.

-oppure-

Il parametro newPasswordQuestion è una stringa vuota.

-oppure-

Il parametro newPasswordAnswer è una stringa vuota.

password è null.

Questo metodo non è disponibile. Ciò può verificarsi se l'applicazione è destinata al profilo client .NET Framework 4. Per evitare questa eccezione, eseguire l'override del metodo o modificare l'applicazione in modo che usi la versione completa di .NET Framework come destinazione.

Esempio

L'esempio di codice seguente aggiorna la domanda e la risposta della password per l'utente connesso corrente.

Importante

Questo esempio contiene una casella di testo che accetta l'input dell'utente, che rappresenta una potenziale minaccia per la sicurezza. Per impostazione predefinita, le pagine Web ASP.NET verificano che l'input dell'utente non includa script o elementi HTML. Per altre informazioni, vedere Cenni preliminari sugli attacchi tramite script.


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

Commenti

ChangePasswordQuestionAndAnswer chiama il ChangePasswordQuestionAndAnswer metodo del provider di appartenenze a cui fa riferimento la ProviderName proprietà per aggiornare la domanda e la risposta della password per l'utente di appartenenza nell'archivio dati di appartenenza.

Il provider di appartenenze può avere restrizioni sulle dimensioni della domanda e della risposta della password. Per le limitazioni delle dimensioni, vedere la documentazione relativa al provider di appartenenze.

Si applica a

Vedi anche