MembershipUser.ChangePasswordQuestionAndAnswer(String, String, String) Método

Definição

Atualiza a pergunta e resposta de senha do usuário associado no armazenamento de dados de associação.

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

Parâmetros

password
String

A senha atual do usuário associado.

newPasswordQuestion
String

O novo valor da pergunta de senha do usuário associado.

newPasswordAnswer
String

O novo valor da resposta de senha do usuário associado.

Retornos

true se a atualização for bem-sucedida, caso contrário, false.

Exceções

password é uma cadeia de caracteres vazia.

- ou -

newPasswordQuestion é uma cadeia de caracteres vazia.

- ou -

newPasswordAnswer é uma cadeia de caracteres vazia.

password é null.

Esse método não está disponível. Isso pode ocorrer se o aplicativo for direcionado ao perfil do cliente .NET Framework 4. Para evitar essa exceção, substitua o método ou altere o aplicativo para ser direcionado à versão completa do .NET Framework.

Exemplos

O exemplo de código a seguir atualiza a pergunta e a resposta de senha para o usuário conectado atual.

Importante

Este exemplo contém uma caixa de texto que aceita a entrada do usuário, que é uma possível ameaça à segurança. Por padrão, ASP.NET páginas da Web validam que a entrada do usuário não inclui elementos HTML ou script. Para obter mais informações, consulte Visão geral de explorações de 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>

Comentários

ChangePasswordQuestionAndAnswer chama o ChangePasswordQuestionAndAnswer método do provedor de associação referenciado pela ProviderName propriedade para atualizar a pergunta e resposta de senha para o usuário associado no armazenamento de dados de associação.

O provedor de associação pode ter restrições sobre o tamanho da pergunta e resposta da senha. Para obter limitações de tamanho, consulte a documentação do provedor de associação.

Aplica-se a

Confira também