AuthenticationService.Authenticating 事件

定义

当验证用户凭据时发生。Occurs when user credentials are being validated.

public:
 static event EventHandler<System::Web::ApplicationServices::AuthenticatingEventArgs ^> ^ Authenticating;
public static event EventHandler<System.Web.ApplicationServices.AuthenticatingEventArgs> Authenticating;
member this.Authenticating : EventHandler<System.Web.ApplicationServices.AuthenticatingEventArgs> 
Public Shared Custom Event Authenticating As EventHandler(Of AuthenticatingEventArgs) 

事件类型

EventHandler<AuthenticatingEventArgs>

示例

下面的示例演示如何 Authenticating 在 global.asax 文件的方法中绑定事件的事件处理程序 Application_StartThe following example shows how to bind an event handler for the Authenticating event in the Application_Start method of the Global.asax file.

void Application_Start(object sender, EventArgs e) 
{
    System.Web.ApplicationServices.AuthenticationService.Authenticating += 
        new EventHandler<System.Web.ApplicationServices.AuthenticatingEventArgs>(AuthenticationService_Authenticating);

}
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    AddHandler System.Web.ApplicationServices.AuthenticationService.Authenticating, _
      AddressOf Me.AuthenticationService_Authenticating
End Sub

下面的示例演示了 global.asax 文件中的事件的事件处理程序 AuthenticatingThe following example shows an event handler for the Authenticating event in the Global.asax file. 该事件处理程序从属性中读取两个身份验证值 CustomCredential ,并将用户名和密码传递给名为的自定义身份验证类 StudentAuthenticationThe event handler reads two authentication values from the CustomCredential property and passes them together with the user name and password to a custom authentication class named StudentAuthentication.

void AuthenticationService_Authenticating(object sender, System.Web.ApplicationServices.AuthenticatingEventArgs e)
{
    string studentid = String.Empty;
    string answer = String.Empty;

    string[] credentials =
        e.CustomCredential.Split(new char[] { ',' });
    if (credentials.Length > 0)
    {
        studentid = credentials[0];
        if (credentials.Length > 1)
        {
            answer = credentials[1];
        }
    }

    try
    {
        e.Authenticated =
            StudentAuthentication.ValidateStudentCredentials
            (e.UserName, e.Password, studentid, answer);
    }
    catch (ArgumentNullException ex)
    {
        e.Authenticated = false;
    }

    e.AuthenticationIsComplete = true;
}
Sub AuthenticationService_Authenticating _
   (ByVal sender As Object, _
    ByVal e As System.Web.ApplicationServices.AuthenticatingEventArgs)
    Dim studentid As String = String.Empty
    Dim answer As String = String.Empty

    Dim credentials As String() = _
         e.CustomCredential.Split(New Char() {","c})
    If (credentials.Length > 0) Then
        studentid = credentials(0)
        If (credentials.Length > 1) Then
            answer = credentials(1)
        End If
    End If

    Try
        e.Authenticated = _
            StudentAuthentication.ValidateStudentCredentials _
            (e.Username, e.Password, studentid, answer)
    Catch ex As ArgumentNullException
        e.Authenticated = False
    End Try
    

    e.AuthenticationIsComplete = True
End Sub

注解

Authenticating验证用户凭据时,将引发事件。The Authenticating event is raised when user credentials are being validated. 为事件创建事件处理程序 Authenticating ,以自定义验证用户凭据的方式。Create an event handler for the Authenticating event to customize how user credentials are validated.

适用于

另请参阅