Login.LoggingIn 事件

定義

發生於使用者送出登入資訊之後,進行驗證之前。

public:
 event System::Web::UI::WebControls::LoginCancelEventHandler ^ LoggingIn;
public event System.Web.UI.WebControls.LoginCancelEventHandler LoggingIn;
member this.LoggingIn : System.Web.UI.WebControls.LoginCancelEventHandler 
Public Custom Event LoggingIn As LoginCancelEventHandler 

事件類型

範例

下列程式碼範例會使用 LoggingIn 事件,以確保使用者已在 屬性中 UserName 輸入格式正確的電子郵件地址。 如果沒有,事件 LoggingIn 會取消登入嘗試,並使用 屬性顯示錯誤訊息 InstructionText

<%@ Page Language="C#" %>
<%@ Import Namespace="System.ComponentModel" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

bool IsValidEmail(string strIn)
{
    // Return true if strIn is in valid email 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})(\]?)$"); 
}

void OnLoggingIn(object sender, System.Web.UI.WebControls.LoginCancelEventArgs e)
{
    if (!IsValidEmail(Login1.UserName))
    {
        Login1.InstructionText = "You must enter a valid email address.";
        e.Cancel = true;
    }
    else
    {
        Login1.InstructionText = String.Empty;
    }
}

</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:Login id="Login1" runat="server" 
                OnLoggingIn="OnLoggingIn" 
                UserNameLabelText="Email Address:" 
                UserNameRequiredErrorMessage="Email Address.">
            </asp:Login>
        </form>
    </body>
</html>
<%@ Page Language="VB" %>
<%@ Import Namespace="System.ComponentModel" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Function IsValidEmail(ByVal strIn As String) As Boolean
    ' Return true if strIn is in valid email 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

Sub OnLoggingIn(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LoginCancelEventArgs)
    If Not IsValidEmail(Login1.UserName) Then
        Login1.InstructionText = "You must enter a valid email address."
        e.Cancel = True
    Else
        Login1.InstructionText = String.Empty
    End If
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:Login id="Login1" runat="server" 
                OnLoggingIn="OnLoggingIn" 
                UserNameLabelText="Email Address:" 
                UserNameRequiredErrorMessage="Email Address.">
            </asp:Login>
        </form>
    </body>
</html>

備註

LoggingIn當使用者提交登入資訊,但在網站驗證使用者之前,就會引發此事件。 LoggingIn使用 事件來設定驗證使用者之前所需的任何資訊。

您可以將 物件的 屬性 CancelEventArgs 設定 Canceltrue ,以取消事件期間的 LoggingIn 登入嘗試。

LoggingIn引發事件之後, Login 控制項會 Authenticate 引發 事件,然後引發 LoggedIn 事件。

如需處理事件的詳細資訊,請參閱 處理和引發事件

適用於

另請參閱