This documentation is archived and is not being maintained.

Login.OnLoggingIn Method

Raises the LoggingIn event when a user submits login information but before the authentication takes place.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

Protected Overridable Sub OnLoggingIn ( _
	e As LoginCancelEventArgs _


Type: System.Web.UI.WebControls.LoginCancelEventArgs
A LoginCancelEventArgs containing the event data.

The OnLoggingIn method raises the LoggingIn event. Use the LoggingIn event to perform any processing that you need before authenticating a user or to perform custom validation.

Raising an event invokes the event handler through a delegate. For more information, see Handling and Raising Events.

The OnLoggingIn method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.

Notes to Inheritors

When overriding OnLoggingIn in a derived class, be sure to call the base class's OnLoggingIn method so that registered delegates receive the event.

The following code example uses the LoggingIn event to ensure that the user has entered a well-formed e-mail address in the UserName property. If not, the LoggingIn event handler cancels the login attempt and displays the error message specified in the InstructionText property.

<%@ page language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script runat="server">
    ' This custom Login control checks the user name
    ' entered by the user is a valid e-mail address.
    Class CustomLogin
        Inherits Login

        Function IsValidEmail(ByVal strIn As String) As Boolean
            ' Return true if strIn is in valid e-mail format.
            Return Regex.IsMatch(strIn, ("^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"))
        End Function

        Overrides Protected Sub OnLoggingIn(ByVal e As System.Web.UI.WebControls.LoginCancelEventArgs)
            If Not IsValidEmail(UserName) Then
                InstructionText = "You must enter a valid e-mail address."
                e.Cancel = True
                InstructionText = String.Empty
            End If
        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"

    End Sub

<html xmlns="" >
<head runat="server">
    <title>ASP.NET Example</title>
<form id="Form1" runat="server">
    <asp:placeholder id="Placeholder1" runat="Server"></asp:placeholder>

The following code example demonstrates how you can extend the Login control. The CustomLogin control includes a DropDownList control that lets users choose which membership provider they are authenticated with. (These providers are configured in Web.config.) In the OnLoggingIn method, the MembershipProvider property is set to the selected value of the DropDownList control.

Security noteSecurity Note

This example contains a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls

Namespace Samples.AspNet.Controls

    NotInheritable Public Class CustomLogin
        Inherits Login

        Public Sub New() 
        End Sub 'New

        Protected Overrides Sub OnLoggingIn(ByVal e As LoginCancelEventArgs) 

            ' Set the Membership provider for the Login control from a DropDownList.
            Dim list As DropDownList = CType(Me.FindControl("domain"), DropDownList)
            Me.MembershipProvider = list.SelectedValue

        End Sub 'OnLoggingIn

        Protected Overrides Sub CreateChildControls() 

            LayoutTemplate = New MyTemplate()

        End Sub 'CreateChildControls
    End Class 'CustomLogin

    ' A Template that contains the child controls.
    Public Class MyTemplate
        Implements ITemplate

        Sub InstantiateIn(ByVal container As Control)  Implements ITemplate.InstantiateIn
            ' A TextBox for the user name.
            Dim username As New TextBox()
            username.ID = "username"

            ' A TextBox for the password.
            Dim password As New TextBox()
            password.ID = "password"

            ' A CheckBox to remember the user on subsequent visits.
            Dim remember As New CheckBox()
            remember.ID = "RememberMe"
            remember.Text = "Don't forget me!"

            ' Failure Text.
            Dim failure As New Literal()
            failure.ID = "FailureText"

            ' A DropDownList to choose the Membership provider.
            Dim domain As New DropDownList()
            domain.ID = "Domain"
            domain.Items.Add(New ListItem("SqlMembers"))
            domain.Items.Add(New ListItem("SqlMembers2"))

            ' A Button to log in.
            Dim submit As New Button()
            submit.CommandName = "login"
            submit.Text = "LOGIN"

            container.Controls.Add(New LiteralControl("UserName:"))
            container.Controls.Add(New LiteralControl("<br>Password:"))
            container.Controls.Add(New LiteralControl("<br>"))
            container.Controls.Add(New LiteralControl("<br>Domain:"))
            container.Controls.Add(New LiteralControl("<br>"))
            container.Controls.Add(New LiteralControl("<br>"))

        End Sub 'ITemplate.InstantiateIn
    End Class 'MyTemplate
End Namespace

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.