Share via


Login.OnAuthenticate(AuthenticateEventArgs) Metodo

Definizione

Genera l'evento Authenticate per autenticare l'utente.

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)

Parametri

e
AuthenticateEventArgs

Oggetto AuthenticateEventArgs che contiene i dati dell'evento.

Esempio

Nell'esempio di codice seguente viene usato l'evento per chiamare codice Authenticate di autenticazione personalizzato specifico del sito.

<%@ 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>

Commenti

Il metodo OnAuthenticate genera l'evento Authenticate. Usare l'evento Authenticate per implementare uno schema di autenticazione personalizzato.

Quando viene generato un evento, il gestore dell'evento viene richiamato tramite un delegato. Per altre informazioni, vedere la gestione e generazione di eventi.

Il metodo OnAuthenticate consente inoltre alle classi derivate di gestire l'evento senza associare un delegato. È la tecnica consigliata per la gestione dell'evento in una classe derivata.

Note per gli eredi

Quando si esegue l'override di OnAuthenticate(AuthenticateEventArgs) in una classe derivata, verificare di chiamare il metodo OnAuthenticate(AuthenticateEventArgs) della classe di base in modo che i delegati registrati ricevano l'evento.

Si applica a

Vedi anche