ChangePassword.OnChangingPassword(LoginCancelEventArgs) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在成員資格提供者變更使用者的密碼之前,引發 ChangingPassword 事件。
protected:
virtual void OnChangingPassword(System::Web::UI::WebControls::LoginCancelEventArgs ^ e);
protected virtual void OnChangingPassword (System.Web.UI.WebControls.LoginCancelEventArgs e);
abstract member OnChangingPassword : System.Web.UI.WebControls.LoginCancelEventArgs -> unit
override this.OnChangingPassword : System.Web.UI.WebControls.LoginCancelEventArgs -> unit
Protected Overridable Sub OnChangingPassword (e As LoginCancelEventArgs)
參數
包含事件資料的 CancelEventArgs 物件。
範例
下列程式碼範例示範如何使用使用控制項的 ASP.NET 網頁 ChangePassword ,並包含名為 ChangingPassword
之事件的處理常式 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>
備註
呼叫 OnChangingPassword 屬性中指定的 MembershipProvider 成員資格提供者之前會呼叫 方法,以變更使用者的密碼。
OnChangingPassword使用 方法來執行變更密碼之前所需的任何處理,例如檢查新密碼以確定它不在一般密碼清單中。
方法 OnChangingPassword 可以藉由將傳遞為e參數的物件屬性 CancelEventArgs 設定 Cancel 為 true
來取消 ChangingPassword 事件。
引發事件會透過委派叫用此事件處理常式。 如需詳細資訊,請參閱ASP.NET Web Forms 頁面中的伺服器事件處理。
OnChangingPassword 方法也允許衍生類別處理事件,而不用附加委派。 這是在衍生類別中處理事件的慣用技巧。
給繼承者的注意事項
在衍生類別中覆 OnChangingPassword(LoginCancelEventArgs) 寫 方法時,請務必呼叫 OnChangingPassword(LoginCancelEventArgs) 基類的 方法,讓已註冊的委派接收事件。
適用於
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應