SqlMembershipProvider.ChangePasswordQuestionAndAnswer Metodo

Definizione

Aggiorna la domanda e la risposta relative alla password di un utente nel database di appartenenze 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

Parametri

username
String

Utente per cui modificare la domanda e la risposta per la password.

password
String

Password dell'utente specificato.

newPasswordQuestion
String

Nuova domanda per la password per l'utente specificato.

newPasswordAnswer
String

Nuova risposta per la password per l'utente specificato.

Restituisce

Boolean

true se l'aggiornamento è stato eseguito correttamente. In caso contrario, false. Il valore false viene restituito anche nel caso in cui il parametro password non è corretto oppure se l'utente è bloccato o non esiste nel database.

Eccezioni

username è una stringa vuota (""), contiene una virgola o è di lunghezza superiore a 256 caratteri.

-oppure- password è una stringa vuota o è più lunga di 128 caratteri.

-oppure- newPasswordQuestion è una stringa vuota o è più lunga di 256 caratteri.

-oppure- newPasswordAnswer è una stringa vuota o è più lunga di 128 caratteri.

-oppure- La lunghezza della versione codificata di newPasswordAnswer è maggiore di 128 caratteri.

username è null.

-oppure- password è null.

-oppure- newPasswordQuestion è null e RequiresQuestionAndAnswer è true.

-oppure- newPasswordAnswer è null e RequiresQuestionAndAnswer è true.

Si è verificato un errore durante la modifica della domanda e della risposta relative alla password nel database.

Esempio

Nell'esempio di codice seguente viene aggiornata la domanda e la risposta della password per un utente.

Nota

In questo esempio viene utilizzata la Provider proprietà della Membership classe per chiamare l'oggetto defaultProvider SqlMembershipProvider specificato come nel file Web.config. Se è necessario accedere al provider predefinito come tipo SqlMembershipProvider, è possibile eseguire il cast della Provider proprietà della Membership classe. Per accedere ad altri provider configurati come tipo di provider specifico, è possibile accedervi tramite il nome configurato con la proprietà della Membership classe e eseguirne il Providers cast come tipo di provider specifico.

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

Commenti

Questo metodo viene chiamato dalla MembershipUser classe per aggiornare la domanda e la risposta della password per un utente nel database SQL Server specificato nel file di configurazione dell'applicazione ASP.NET (Web.config). La risposta alla password viene crittografata usando il formato specificato nella PasswordFormat proprietà .

La richiesta di una domanda e una risposta alla password offrono un ulteriore livello di sicurezza durante il recupero o la reimpostazione della password di un utente. Quando si crea un nome utente, un utente può fornire una domanda e una risposta che può essere usata in un secondo momento per recuperare o reimpostare una password dimenticata. Il ChangePasswordQuestionAndAnswer metodo aggiorna la domanda e la risposta della password per un utente di appartenenza.

Se al metodo viene fornita ChangePasswordQuestionAndAnswer una password errata, i contatori interni che tengono traccia dei tentativi di password non validi vengono incrementati di uno. Ciò può comportare il blocco dell'utente e l'impossibilità di accedere fino a quando lo stato del blocco non viene cancellato chiamando il UnlockUser metodo . Se viene specificata la password corretta e l'utente non è attualmente bloccato, i contatori interni che tengono traccia dei tentativi di password e risposta password non validi vengono reimpostati su zero. Per altre informazioni, vedere le MaxInvalidPasswordAttempts proprietà e PasswordAttemptWindow .

La lunghezza massima per la domanda relativa alla password è di 256 caratteri. La lunghezza massima per la risposta password è di 128 caratteri.

Per altre informazioni, vedere RequiresQuestionAndAnswer, ResetPassword e GetPassword.

Gli spazi iniziali e finali vengono tagliati da tutti i valori dei parametri.

Si applica a

Vedi anche