Assembly: System.Web (in system.web.dll)
Public Overridable ReadOnly Property PasswordQuestion As String
Dim instance As MembershipUser Dim value As String value = instance.PasswordQuestion
public virtual string PasswordQuestion { get; }
public: virtual property String^ PasswordQuestion { String^ get (); }
/** @property */ public String get_PasswordQuestion ()
public function get PasswordQuestion () : String
Property Value
The password question for the membership user.If RequiresQuestionAndAnswer is true, then the password answer for a membership user must be supplied to the GetPassword and ResetPassword methods. The PasswordQuestion serves as a reminder to the membership user of what his or her password answer is. Once the membership user has supplied a password answer, the answer value can be supplied to the GetPassword or ResetPassword method.
The following code example resets a user's password if he or she supplies the appropriate password answer. The PasswordQuestion for the user is displayed as a reminder to the user of the password answer.
<%@ Page Language="VB" %> <%@ Import Namespace="System.Web.Security" %> <script runat="server"> Dim u As MembershipUser Public Sub Page_Load(sender As Object, args As EventArgs) If Not Membership.EnablePasswordReset Then FormsAuthentication.RedirectToLoginPage() End If Msg.Text = "" If Not IsPostBack Then Msg.Text = "Please supply a username." Else VerifyUsername() End If End Sub Public Sub VerifyUsername() u = Membership.GetUser(UsernameTextBox.Text, False) If u Is Nothing Then Msg.Text = "Username " & Server.HtmlEncode(UsernameTextBox.Text) & " not found. Please check the value and re-enter." QuestionLabel.Text = "" QuestionLabel.Enabled = False AnswerTextBox.Enabled = False ResetPasswordButton.Enabled = False Else QuestionLabel.Text = u.PasswordQuestion QuestionLabel.Enabled = True AnswerTextBox.Enabled = True ResetPasswordButton.Enabled = True End If End Sub Public Sub ResetPassword_OnClick(sender As Object, args As EventArgs) Dim newPassword As String u = Membership.GetUser(UsernameTextBox.Text, False) If u Is Nothing Then Msg.Text = "Username " & Server.HtmlEncode(UsernameTextBox.Text) & " not found. Please check the value and re-enter." Return End If Try newPassword = u.ResetPassword(AnswerTextBox.Text) Catch e As MembershipPasswordException Msg.Text = "Invalid password answer. Please re-enter and try again." Return Catch e As Exception Msg.Text = e.Message Return End Try If Not newPassword Is Nothing Then Msg.Text = "Password reset. Your new password is: " & Server.HtmlEncode(newPassword) Else Msg.Text = "Password reset failed. Please re-enter your values and try again." End If End Sub </script> <html> <head> <title>Sample: Reset Password</title> </head> <body> <form runat="server"> <h3>Retrieve Password</h3> <asp:Label id="Msg" runat="server" ForeColor="maroon" /><BR> Username: <asp:Textbox id="UsernameTextBox" Columns="30" runat="server" AutoPostBack="True" /> <asp:RequiredFieldValidator id="UsernameRequiredValidator" runat="server" ControlToValidate="UsernameTextBox" ForeColor="red" Display="Static" ErrorMessage="Required" /><BR> Password Question: <B><asp:Label id="QuestionLabel" runat="server" /></B><BR> Answer: <asp:TextBox id="AnswerTextBox" Columns="60" runat="server" Enabled="False" /> <asp:RequiredFieldValidator id="AnswerRequiredValidator" runat="server" ControlToValidate="AnswerTextBox" ForeColor="red" Display="Static" ErrorMessage="Required" Enabled="False" /><BR> <asp:Button id="ResetPasswordButton" Text="Reset Password" OnClick="ResetPassword_OnClick" runat="server" Enabled="False" /> </form> </body> </html>
<%@ Page Language="C#" %> <%@ Import Namespace="System.Web.Security" %> <script runat="server"> MembershipUser u; public void Page_Load(object sender, EventArgs args) { if (!Membership.EnablePasswordReset) { FormsAuthentication.RedirectToLoginPage(); } Msg.Text = ""; if (!IsPostBack) { Msg.Text = "Please supply a username."; } else { VerifyUsername(); } } public void VerifyUsername() { u = Membership.GetUser(UsernameTextBox.Text, false); if (u == null) { Msg.Text = "Username " + Server.HtmlEncode(UsernameTextBox.Text) + " not found. Please check the value and re-enter."; QuestionLabel.Text = ""; QuestionLabel.Enabled = false; AnswerTextBox.Enabled = false; ResetPasswordButton.Enabled = false; } else { QuestionLabel.Text = u.PasswordQuestion; QuestionLabel.Enabled = true; AnswerTextBox.Enabled = true; ResetPasswordButton.Enabled = true; } } public void ResetPassword_OnClick(object sender, EventArgs args) { string newPassword; u = Membership.GetUser(UsernameTextBox.Text, false); if (u == null) { Msg.Text = "Username " + Server.HtmlEncode(UsernameTextBox.Text) + " not found. Please check the value and re-enter."; return; } try { newPassword = u.ResetPassword(AnswerTextBox.Text); } catch (MembershipPasswordException e) { Msg.Text = "Invalid password answer. Please re-enter and try again."; return; } catch (Exception e) { Msg.Text = e.Message; return; } if (newPassword != null) { Msg.Text = "Password reset. Your new password is: " + Server.HtmlEncode(newPassword); } else { Msg.Text = "Password reset failed. Please re-enter your values and try again."; } } </script> <html> <head> <title>Sample: Reset Password</title> </head> <body> <form runat="server"> <h3>Retrieve Password</h3> <asp:Label id="Msg" runat="server" ForeColor="maroon" /><BR> Username: <asp:Textbox id="UsernameTextBox" Columns="30" runat="server" AutoPostBack="true" /> <asp:RequiredFieldValidator id="UsernameRequiredValidator" runat="server" ControlToValidate="UsernameTextBox" ForeColor="red" Display="Static" ErrorMessage="Required" /><BR> Password Question: <B><asp:Label id="QuestionLabel" runat="server" /></B><BR> Answer: <asp:TextBox id="AnswerTextBox" Columns="60" runat="server" Enabled="false" /> <asp:RequiredFieldValidator id="AnswerRequiredValidator" runat="server" ControlToValidate="AnswerTextBox" ForeColor="red" Display="Static" ErrorMessage="Required" Enabled="false" /><BR> <asp:Button id="ResetPasswordButton" Text="Reset Password" OnClick="ResetPassword_OnClick" runat="server" Enabled="false" /> </form> </body> </html>
Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.
.NET Framework
Supported in: 2.0Reference
MembershipUser ClassMembershipUser Members
System.Web.Security Namespace