MembershipUser.ChangePasswordQuestionAndAnswer(String, String, String) 方法
定义
为成员资格数据存储中的成员资格用户更新密码问题和答案。Updates the password question and answer for the membership user in the membership data store.
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
参数
- password
- String
成员资格用户的当前密码。The current password for the membership user.
- newPasswordQuestion
- String
成员资格用户的新密码问题值。The new password question value for the membership user.
- newPasswordAnswer
- String
成员资格用户的新密码答案值。The new password answer value for the membership user.
返回
如果更新操作成功,则为 true;否则为 false。true if the update was successful; otherwise, false.
例外
password 是一个空字符串。password is an empty string.
- 或 --or-
newPasswordQuestion 是一个空字符串。newPasswordQuestion is an empty string.
- 或 --or-
newPasswordAnswer 是一个空字符串。newPasswordAnswer is an empty string.
password 为 null。password is null.
此方法不可用。This method is not available. 如果应用程序以 .NET Framework 4 客户端配置文件为目标,则可能会发生这种情况。This can occur if the application targets the .NET Framework 4 Client Profile. 若要避免此异常,请重写该方法,或将应用程序更改为面向 .NET Framework 的完整版本。To prevent this exception, override the method, or change the application to target the full version of the .NET Framework.
示例
下面的代码示例将更新当前已登录用户的密码提示问题和答案。The following code example updates the password question and answer for the current logged-on user.
重要
此示例包含一个文本框,该文本框接受用户输入,这是一个潜在的安全威胁。This example contains a text box that accepts user input, which is a potential security threat. 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。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"
"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>
注解
ChangePasswordQuestionAndAnswer 调用 ChangePasswordQuestionAndAnswer 由属性引用的成员资格提供程序的方法, ProviderName 以便为成员资格数据存储区中的成员资格用户更新密码提示问题和答案。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.