DefaultAuthenticationEventHandler 委托

定义

表示处理 DefaultAuthenticationModule 的 DefaultAuthentication_OnAuthenticate 事件的方法。Represents the method that handles the DefaultAuthentication_OnAuthenticate event of a DefaultAuthenticationModule.

public delegate void DefaultAuthenticationEventHandler(System::Object ^ sender, DefaultAuthenticationEventArgs ^ e);
public delegate void DefaultAuthenticationEventHandler(object sender, DefaultAuthenticationEventArgs e);
type DefaultAuthenticationEventHandler = delegate of obj * DefaultAuthenticationEventArgs -> unit
Public Delegate Sub DefaultAuthenticationEventHandler(sender As Object, e As DefaultAuthenticationEventArgs)

参数

sender
Object

事件源。The source of the event.

e
DefaultAuthenticationEventArgs

包含事件数据的 DefaultAuthenticationEventArgsA DefaultAuthenticationEventArgs that contains the event data.

继承
DefaultAuthenticationEventHandler

示例

下面的代码示例使用DefaultAuthentication_OnAuthenticate事件来测试User当前HttpContext的属性是否为nullThe following code example uses the DefaultAuthentication_OnAuthenticate event to test whether the User property of the current HttpContext is null. 如果null属性为, 则此示例User将当前HttpContext的属性设置GenericPrincipalGenericPrincipal一个Identity GenericIdentity对象, 其中对象的是一个值为"default"的Name".If the property is null, the sample sets the User property of the current HttpContext to a GenericPrincipal object, where the Identity of the GenericPrincipal object is a GenericIdentity with a Name value of "default".

备注

AuthorizeRequest事件前引发 DefaultAuthentication_OnAuthenticate 事件。The DefaultAuthentication_OnAuthenticate event is raised before the AuthorizeRequest event. 因此, 如果将当前User HttpContext的属性设置为自定义标识, 则它可能会影响应用程序的行为。As a result, if you set the User property of the current HttpContext to a custom identity, it can affect the behavior of your application. 例如, 如果您使用的FormsAuthentication是类, 并且您确保只有通过身份验证的用户才能访问您的网站, 则使用部分并指定<deny users="?" />, 此示例将导致元素被忽略, 因为用户将具有一个名称, 该名称为 "default"。For example, if you are using the FormsAuthentication class and you are ensuring that only authenticated users have access to your site, by using the section and specifying <deny users="?" />, this sample will cause the element to be ignored, as the user will have a name, which is "default". 相反, 您需要<deny users="default" />指定以确保只有经过身份验证的用户才能访问您的站点。Instead you would specify <deny users="default" /> to ensure that only authenticated users can access your site.

public void DefaultAuthentication_OnAuthenticate(object sender,
                                                 DefaultAuthenticationEventArgs args)
{
  if (args.Context.User == null)
    args.Context.User = 
      new System.Security.Principal.GenericPrincipal(
        new System.Security.Principal.GenericIdentity("default"),
        new String[0]);
}
Public Sub DefaultAuthentication_OnAuthenticate(sender As Object, _
                                                args As DefaultAuthenticationEventArgs)
  If args.Context.User Is Nothing Then
    args.Context.User = _
      new System.Security.Principal.GenericPrincipal( _
        new System.Security.Principal.GenericIdentity("default"), _
        new String(0) {})
  End If
End Sub

注解

委托是DefaultAuthenticationModule Authenticate为类的事件定义的。 DefaultAuthenticationEventHandlerThe DefaultAuthenticationEventHandler delegate is defined for the Authenticate event of the DefaultAuthenticationModule class. 可以通过在 ASP.NET Authenticate应用程序的DefaultAuthenticationModule global.asax 文件中指定名为DefaultAuthentication_OnAuthenticate的子例程来访问类的事件。You can access the Authenticate event of the DefaultAuthenticationModule class by specifying a subroutine named DefaultAuthentication_OnAuthenticate in the Global.asax file for your ASP.NET application. AuthenticateRequest User HttpContext事件在事件后引发, 并用于确保使用IPrincipal对象填充当前的属性。 AuthenticateThe Authenticate event is raised after the AuthenticateRequest event and is used to ensure that the User property of the current HttpContext is populated with an IPrincipal object.

可以使用提供给Context DefaultAuthentication_OnAuthenticate事件UserDefaultAuthenticationEventArgs对象的属性, 将当前HttpContext的属性设置为自定义IPrincipal对象。You can use the Context property of the DefaultAuthenticationEventArgs object supplied to the DefaultAuthentication_OnAuthenticate event to set the User property of the current HttpContext to a custom IPrincipal object. User如果未在DefaultAuthenticationModule DefaultAuthentication_OnAuthenticate 事件期间为提供的HttpContext属性指定值, 则会将的HttpContext 属性设置为GenericPrincipal User不包含用户信息的对象。If you do not specify a value for the User property of the HttpContext supplied during the DefaultAuthentication_OnAuthenticate event, the DefaultAuthenticationModule sets the User property of the HttpContext to a GenericPrincipal object that contains no user information.

在事件之后AuthenticateRequestAuthorizeRequest事件发生之前引发 DefaultAuthentication_OnAuthenticate 事件。The DefaultAuthentication_OnAuthenticate event is raised after the AuthenticateRequest event and before the AuthorizeRequest event. 如果某个节依赖于要拒绝的用户名或允许对应用程序的访问, 则修改User当前HttpContext的属性可能会影响应用程序的行为。If you have an section that depends on the user name to deny or allow access to your application, modifying the User property of the current HttpContext can affect the behavior of your application. 在配置中指定节时, 请确保考虑在DefaultAuthentication_OnAuthenticate事件中设置的用户名。Be sure that the user name you set during the DefaultAuthentication_OnAuthenticate event is considered when you specify the section in your configuration.

扩展方法

GetMethodInfo(Delegate)

获取指示指定委托表示的方法的对象。Gets an object that represents the method represented by the specified delegate.

适用于