MembershipUser.ResetPassword 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将用户密码重置为自动生成的新密码。
重载
ResetPassword() |
将用户密码重置为自动生成的新密码。 |
ResetPassword(String) |
将用户密码重置为自动生成的新密码。 |
ResetPassword()
将用户密码重置为自动生成的新密码。
public:
virtual System::String ^ ResetPassword();
public virtual string ResetPassword ();
abstract member ResetPassword : unit -> string
override this.ResetPassword : unit -> string
Public Overridable Function ResetPassword () As String
返回
成员资格用户的新密码。
例外
此方法不可用。 如果应用程序面向 .NET Framework 4 客户端配置文件,则可能会发生这种情况。 若要避免此异常,请重写该方法,或将应用程序更改为面向 .NET Framework 的完整版本。
示例
下面的代码示例重置用户的密码,并返回自动生成的新密码。 请注意, RequiresQuestionAndAnswer 假定为 false
。
重要
此示例包含一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述。
<%@ 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">
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.";
ResetPasswordButton.Enabled = false;
}
else
{
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();
}
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 xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Sample: Reset Password</title>
</head>
<body>
<form id="form1" 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 />
<asp:Button id="ResetPasswordButton" Text="Reset Password"
OnClick="ResetPassword_OnClick" runat="server" Enabled="false" />
</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">
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."
ResetPasswordButton.Enabled = False
Else
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()
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 xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Sample: Reset Password</title>
</head>
<body>
<form id="form1" 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 />
<asp:Button id="ResetPasswordButton" Text="Reset Password"
OnClick="ResetPassword_OnClick" runat="server" Enabled="False" />
</form>
</body>
</html>
注解
ResetPassword MembershipProvider.ResetPassword调用 属性引用ProviderName的成员资格提供程序的 方法,以将成员身份用户的密码重置为自动生成的新密码。 然后,新密码将返回到调用方。
如果 EnablePasswordReset 为 false
,则成员资格提供程序将返回异常。
如果 RequiresQuestionAndAnswer 为 true
,则必须使用 ResetPassword 采用密码答案作为参数的重载,并为成员身份用户提供密码答案。 如果需要密码答案,并且提供了不正确的密码答案, MembershipPasswordException 则成员资格提供程序会引发 。
另请参阅
适用于
ResetPassword(String)
将用户密码重置为自动生成的新密码。
public:
virtual System::String ^ ResetPassword(System::String ^ passwordAnswer);
public virtual string ResetPassword (string passwordAnswer);
abstract member ResetPassword : string -> string
override this.ResetPassword : string -> string
Public Overridable Function ResetPassword (passwordAnswer As String) As String
参数
- passwordAnswer
- String
成员资格用户的密码答案。
返回
成员资格用户的新密码。
例外
此方法不可用。 如果应用程序面向 .NET Framework 4 客户端配置文件,则可能会发生这种情况。 若要避免此异常,请重写该方法,或将应用程序更改为面向 .NET Framework 的完整版本。
示例
下面的代码示例重置用户的密码,并返回自动生成的新密码。
重要
此示例包含一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述。
<%@ 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">
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 xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Sample: Reset Password</title>
</head>
<body>
<form id="form1" 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="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">
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 xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Sample: Reset Password</title>
</head>
<body>
<form id="form1" 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>
注解
ResetPassword MembershipProvider.ResetPassword调用 属性引用ProviderName的成员资格提供程序的 方法,以将成员身份用户的密码重置为自动生成的新密码。 然后,新密码将返回到调用方。
如果 EnablePasswordReset 为 false
,则成员资格提供程序将返回异常。
如果 RequiresQuestionAndAnswer 为 false
,则可以为 answer
参数提供null
,或使用ResetPassword不采用任何参数的重载。
如果需要密码答案,并且提供了不正确的密码答案, MembershipPasswordException 则成员资格提供程序会引发 。