Login.OnAuthenticate(AuthenticateEventArgs) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
引發 Authenticate 事件來驗證使用者。
protected:
virtual void OnAuthenticate(System::Web::UI::WebControls::AuthenticateEventArgs ^ e);
protected virtual void OnAuthenticate (System.Web.UI.WebControls.AuthenticateEventArgs e);
abstract member OnAuthenticate : System.Web.UI.WebControls.AuthenticateEventArgs -> unit
override this.OnAuthenticate : System.Web.UI.WebControls.AuthenticateEventArgs -> unit
Protected Overridable Sub OnAuthenticate (e As AuthenticateEventArgs)
參數
包含事件資料的 AuthenticateEventArgs。
範例
下列程式代碼範例會 Authenticate 使用 事件來呼叫月臺特定的自定義驗證程序代碼。
<%@ page language="C#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
// This custom login control overloads the OnAuthenticate method
// to call a site-specific authentication method.
class CustomLogin : Login
{
private bool SiteSpecificAuthenticationMethod(string UserName, string Password)
{
// Insert code that implements a site-specific custom
// authentication method here.
//
// This example implementation always returns false.
return false;
}
override protected void OnAuthenticate(AuthenticateEventArgs e)
{
bool Authenticated = false;
Authenticated = SiteSpecificAuthenticationMethod(UserName, Password);
e.Authenticated = Authenticated;
}
}
// Add the custom login control to the page.
void Page_Load(object sender, EventArgs e)
{
CustomLogin loginControl = new CustomLogin();
loginControl.ID = "loginControl";
Placeholder1.Controls.Add(loginControl);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
<asp:placeholder id="Placeholder1" runat="server"></asp:placeholder>
</form>
</body>
</html>
<%@ page language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
' This custom login control overloads the OnAuthenticate method
' to call a site-specific authentication method.
Class CustomLogin
Inherits Login
Private Function SiteSpecificAuthenticationMethod(ByVal UserName As String, ByVal Password As String) As Boolean
' Insert code that implements a site-specific custom
' authentication method here.
'
' This example implementation always returns false.
Return False
End Function
Overloads Sub OnAuthenticate(ByVal e As AuthenticateEventArgs)
Dim Authenticated As Boolean
Authenticated = SiteSpecificAuthenticationMethod(UserName, Password)
e.Authenticated = Authenticated
End Sub
End Class
' Add the custom login control to the page.
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim loginControl As New CustomLogin
loginControl.ID = "loginControl"
PlaceHolder1.Controls.Add(loginControl)
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
<asp:placeholder id="PlaceHolder1" runat="server"></asp:placeholder>
</form>
</body>
</html>
備註
OnAuthenticate 方法會引發 Authenticate 事件。 Authenticate使用事件來實作自訂驗證配置。
引發事件會透過委派叫用此事件處理常式。 如需詳細資訊,請參閱 處理和引發事件。
OnAuthenticate 方法也允許衍生類別處理事件,而不用附加委派。 這是在衍生類別中處理事件的慣用技巧。
給繼承者的注意事項
當在衍生類別中覆寫 OnAuthenticate(AuthenticateEventArgs) 時,請確定呼叫基底類別的 OnAuthenticate(AuthenticateEventArgs) 方法,使已註冊的委派能接收到事件。