AuthenticatingEventArgs.UserName 属性

定义

为用户获取身份验证名称。Gets the authentication name for the user.

public:
 property System::String ^ UserName { System::String ^ get(); };
public string UserName { get; }
member this.UserName : string
Public ReadOnly Property UserName As String

属性值

String

要验证的用户名。The user name to validate.

示例

下面的示例演示了一个事件处理程序,该处理程序根据属性的值选择要使用的成员资格提供程序 UserNameThe following example shows an event handler that selects a membership provider to use based on the value of the UserName property. 处理程序将 UserNamePassword 值传递到自定义成员资格提供程序以验证用户凭据。The handler passes UserName and Password values to the custom membership provider to validate the user credentials. 它将设置 Authenticated 为方法的返回值 ValidateUser ,并将设置 AuthenticationIsComplete 为, true 以使 AuthenticationService 类不验证凭据。It sets Authenticated to the return value of the ValidateUser method and sets AuthenticationIsComplete to true so that the AuthenticationService class does not validate the credentials.

void AuthenticationService_Authenticating(object sender, System.Web.ApplicationServices.AuthenticatingEventArgs e)
{
    if (e.UserName.IndexOf("@contoso.com") >= 0)
    {
        e.Authenticated = Membership.Providers["ContosoSqlProvider"].ValidateUser(e.UserName, e.Password);
    }
    else if (e.UserName.IndexOf("@fabrikam.com") >= 0)
    {
        e.Authenticated = Membership.Providers["FabrikamSqlProvider"].ValidateUser(e.UserName, e.Password);
    }
    else
    {
        e.Authenticated = Membership.Provider.ValidateUser(e.UserName, e.Password);
    }
    e.AuthenticationIsComplete = true;
}
Sub AuthenticationService_Authenticating _
   (ByVal sender As Object, _
    ByVal e As System.Web.ApplicationServices.AuthenticatingEventArgs)
    
    If (e.Username.IndexOf("@contoso.com") >= 0) Then
        e.Authenticated = Membership.Providers("ContosoSqlProvider").ValidateUser(e.Username, e.Password)
    ElseIf (e.Username.IndexOf("@fabrikam.com") >= 0) Then
        e.Authenticated = Membership.Providers("FabrikamSqlProvider").ValidateUser(e.Username, e.Password)
    Else
        e.Authenticated = Membership.Provider.ValidateUser(e.Username, e.Password)
    End If
    e.AuthenticationIsComplete = True
End Sub

注解

UserName 事件期间使用属性检索身份验证名称 AuthenticatingYou use the UserName property to retrieve the authentication name during the Authenticating event.

适用于