ChangePassword.ChangingPassword 事件
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
發生於成員資格提供者變更使用者帳戶密碼之前。
public:
event System::Web::UI::WebControls::LoginCancelEventHandler ^ ChangingPassword;
public event System.Web.UI.WebControls.LoginCancelEventHandler ChangingPassword;
member this.ChangingPassword : System.Web.UI.WebControls.LoginCancelEventHandler
Public Custom Event ChangingPassword As LoginCancelEventHandler
事件類型
範例
下列程式碼範例示範如何使用使用控制項的 ASP.NET 網頁 ChangePassword ,並包含名為 ChangingPassword
之事件的處理常式 ChangingPassword 。 事件處理常式中的程式碼會將 屬性中 CurrentPassword 儲存的舊密碼與 儲存在 中的 NewPassword 新密碼進行比較。 如果兩個密碼相同,變更密碼會失敗。
控制項 ChangePassword 會將 DisplayUserName 屬性設定為 true
,讓使用者輸入其使用者名稱。 這表示使用者不需要登入即可檢視頁面。
程式碼範例假設 ASP.NET 網站已設定為使用 ASP.NET 成員資格和表單驗證,而且已建立使用者的名稱和密碼已知。 如需詳細資訊,請參閱 如何:實作簡單表單驗證。
<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
//Manually register the event-handling methods.
ChangePassword1.ChangingPassword += new LoginCancelEventHandler(this._ChangingPassword);
}
void _ChangingPassword(Object sender, LoginCancelEventArgs e)
{
if (ChangePassword1.CurrentPassword.ToString() == ChangePassword1.NewPassword.ToString())
{
Message1.Visible = true;
Message1.Text = "Old password and new password must be different. Please try again.";
e.Cancel = true;
}
else
{
//This line prevents the error showing up after a first failed attempt.
Message1.Visible = false;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ChangePassword including a ChangingPassword event handler</title>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align:center">
<h1>ChangePassword</h1>
<asp:LoginView ID="LoginView1" Runat="server"
Visible="true">
<LoggedInTemplate>
<asp:LoginName ID="LoginName1" Runat="server" FormatString="You are logged in as {0}." />
<br />
</LoggedInTemplate>
<AnonymousTemplate>
You are not logged in
</AnonymousTemplate>
</asp:LoginView><br />
<asp:ChangePassword ID="ChangePassword1" Runat="server"
BorderStyle="Solid"
BorderWidth="1"
CancelDestinationPageUrl="~/Default.aspx"
DisplayUserName="true"
OnChangingPassword="_ChangingPassword"
ContinueDestinationPageUrl="~/Default.aspx" >
</asp:ChangePassword><br />
<asp:Label ID="Message1" Runat="server" ForeColor="Red" /><br />
<asp:HyperLink ID="HyperLink1" Runat="server"
NavigateUrl="~/Default.aspx">
Home
</asp:HyperLink>
</div>
</form>
</body>
</html>
<%@ Page Language="VB" AutoEventWireup="True" %>
<!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 PageLoad(ByVal Sender As Object, ByVal e As EventArgs)
'Manually register the event-handling methods.
AddHandler ChangePassword1.ChangingPassword, AddressOf Me._ChangingPassword
End Sub
Public Sub _ChangingPassword(ByVal Sender As Object, ByVal e As LoginCancelEventArgs)
If (ChangePassword1.CurrentPassword.ToString() = ChangePassword1.NewPassword.ToString) Then
Message1.Visible = True
Message1.Text = "Old password and new password must be different. Please try again."
e.Cancel = True
Else
'This line prevents the error showing up after a first failed attempt.
Message1.Visible = False
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ChangePassword including a ChangingPassword event handler</title>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align:center">
<h1>ChangePassword</h1>
<asp:LoginView ID="LoginView1" Runat="server"
Visible="true">
<LoggedInTemplate>
<asp:LoginName ID="LoginName1" Runat="server" FormatString="You are logged in as {0}." />
<br />
</LoggedInTemplate>
<AnonymousTemplate>
You are not logged in
</AnonymousTemplate>
</asp:LoginView><br />
<asp:ChangePassword ID="ChangePassword1" Runat="server"
BorderStyle="Solid"
BorderWidth="1"
CancelDestinationPageUrl="~/Default.aspx"
DisplayUserName="true"
OnChangingPassword="_ChangingPassword"
ContinueDestinationPageUrl="~/Default.aspx" >
</asp:ChangePassword><br />
<asp:Label ID="Message1" Runat="server" ForeColor="Red" /><br />
<asp:HyperLink ID="HyperLink1" Runat="server"
NavigateUrl="~/Default.aspx">
Home
</asp:HyperLink>
</div>
</form>
</body>
</html>
備註
在 ChangingPassword 呼叫 屬性中指定的 MembershipProvider 成員資格提供者之前,會引發 事件,以變更使用者帳戶的密碼。
ChangingPassword使用 事件來執行變更密碼之前所需的任何處理,例如檢查新密碼以確定它不在一般密碼清單中。 使用者的新授權權杖會在事件之後 ChangingPassword ,但在事件之前 ChangedPassword 設定。
ChangingPassword如果事件處理常式判斷不應該呼叫成員資格提供者,則可以取消 CancelLoginCancelEventArgstrue
事件。
如需處理事件的詳細資訊,請參閱 處理和引發事件。
適用於
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應