MembershipUser.ChangePasswordQuestionAndAnswer Method (String, String, String)


Updates the password question and answer for the membership user in the membership data store.

Namespace:   System.Web.Security
Assembly:  System.Web.ApplicationServices (in System.Web.ApplicationServices.dll)

public virtual bool ChangePasswordQuestionAndAnswer(
	string password,
	string newPasswordQuestion,
	string newPasswordAnswer


Type: System.String

The current password for the membership user.

Type: System.String

The new password question value for the membership user.

Type: System.String

The new password answer value for the membership user.

Return Value

Type: System.Boolean

true if the update was successful; otherwise, false.

Exception Condition

password is an empty string.


newPasswordQuestion is an empty string.


newPasswordAnswer is an empty string.


password is null.


This method is not available. This can occur if the application targets the .NET Framework 4 Client Profile. To prevent this exception, override the method, or change the application to target the full version of the .NET Framework.

ChangePasswordQuestionAndAnswer calls the ChangePasswordQuestionAndAnswer method of the membership provider referenced by the ProviderName property to update the password question and answer for the membership user in the membership data store.

The membership provider may have restrictions on the size of the password question and answer. For size limitations, see the documentation for the membership provider.

The following code example updates the password question and answer for the current logged-on user.

System_CAPS_security Security Note

This example contains a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

<%@ Page Language="C#" %>

<%@ Import Namespace="System.Web.Security" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script runat="server">

  public void ChangePasswordQuestion_OnClick(object sender, EventArgs args)
      MembershipUser u = Membership.GetUser(User.Identity.Name);
      Boolean result = u.ChangePasswordQuestionAndAnswer(PasswordTextbox.Text,

      if (result)
        Msg.Text = "Password Question and Answer changed.";
        Msg.Text = "Password Question and Answer change failed.";
    catch (Exception e)
      Msg.Text = "Change failed. Please re-enter your values and try again.";


<html xmlns="" >
  <title>Sample: Change Password Question and Answer</title>
  <form id="form1" runat="server">
      Change Password Question and Answer for
    <asp:Label ID="Msg" ForeColor="maroon" runat="server" /><br />
    <table cellpadding="3" border="0">
          <asp:TextBox ID="PasswordTextbox" runat="server" TextMode="Password" /></td>
          <asp:RequiredFieldValidator ID="OldPasswordRequiredValidator" runat="server" ControlToValidate="PasswordTextbox"
            ForeColor="red" Display="Static" ErrorMessage="Required" /></td>
          New Password Question:</td>
          <asp:TextBox ID="QuestionTextbox" MaxLength="256" Columns="60" runat="server" /></td>
          <asp:RequiredFieldValidator ID="QuestionRequiredValidator" runat="server" ControlToValidate="QuestionTextbox"
            ForeColor="red" Display="Static" ErrorMessage="Required" /></td>
          New Password Answer:</td>
          <asp:TextBox ID="AnswerTextbox" MaxLength="128" Columns="60" runat="server" /></td>
          <asp:RequiredFieldValidator ID="AnswerRequiredValidator" runat="server" ControlToValidate="AnswerTextbox"
            ForeColor="red" Display="Static" ErrorMessage="Required" /></td>
          <asp:Button ID="ChangePasswordQuestionButton" Text="Change Password Question and Answer"
            OnClick="ChangePasswordQuestion_OnClick" runat="server" /></td>

.NET Framework
Available since 2.0
Return to top