MembershipUser.ChangePasswordQuestionAndAnswer Method
Assembly: System.Web (in system.web.dll)
public virtual bool ChangePasswordQuestionAndAnswer ( string password, string newPasswordQuestion, string newPasswordAnswer )
public boolean ChangePasswordQuestionAndAnswer ( String password, String newPasswordQuestion, String newPasswordAnswer )
public function ChangePasswordQuestionAndAnswer ( password : String, newPasswordQuestion : String, newPasswordAnswer : String ) : boolean
Not applicable.
Parameters
- password
The current password for the membership user.
- newPasswordQuestion
The new password question value for the membership user.
- newPasswordAnswer
The new password answer value for the membership user.
Return Value
true if the update was successful; otherwise, false.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.
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 (Visual Studio). |
<%@ 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>
Security Note: