DefaultAuthenticationModule.Authenticate 事件

定义

对请求进行身份验证后发生。

public:
 event System::Web::Security::DefaultAuthenticationEventHandler ^ Authenticate;
public event System.Web.Security.DefaultAuthenticationEventHandler Authenticate;
member this.Authenticate : System.Web.Security.DefaultAuthenticationEventHandler 
Public Custom Event Authenticate As DefaultAuthenticationEventHandler 

事件类型

示例

下面的代码示例使用 DefaultAuthentication_OnAuthenticate 事件来测试当前HttpContext实例的 属性是否UsernullUser如果 属性为 null,则示例将当前HttpContext实例的 属性设置为User一个 GenericPrincipal 对象,GenericPrincipal其中 Identity 对象的 是值为GenericIdentityName“default”的 。

注意

DefaultAuthentication_OnAuthenticate 事件在事件之前AuthorizeRequest引发。 因此,如果将当前HttpContext实例的 属性设置为User自定义标识,可能会影响应用程序的行为。 例如,如果使用 FormsAuthentication 类并在授权配置部分中指定<deny users="?" />,以确保只有经过身份验证的用户有权访问您的网站,则此示例将导致拒绝元素被忽略,因为用户将具有一个名称,即“默认值”。相反,应指定 <deny users="default" /> 以确保只有经过身份验证的用户才能访问你的站点。

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

注解

事件 Authenticate 在事件之后 AuthenticateRequest 引发。 它用于确保User使用 对象填充IPrincipal当前HttpContext实例的 属性。

可以通过在应用程序的 Global.asax 文件中指定名为 DefaultAuthentication_OnAuthenticate 的子例程来访问 AuthenticateDefaultAuthenticationModule 类的事件。

可以使用 ContextDefaultAuthentication_OnAuthenticate 事件中 对象的 属性DefaultAuthenticationEventArgs将当前HttpContext实例的 属性设置为User自定义IPrincipal对象。 如果未为 User 属性指定值,则 DefaultAuthenticationModule 将 实例的 HttpContext 属性设置为UserGenericPrincipal不包含用户信息的对象。

DefaultAuthentication_OnAuthenticate 事件在 AuthenticateRequest 事件之后和 事件之前AuthorizeRequest引发。 如果某个 authorization 节依赖于用户名来拒绝或允许访问应用程序,则修改 User 当前 HttpContext 实例的 属性可能会影响应用程序的行为。 在配置中指定授权部分时,请确保考虑在 DefaultAuthentication_OnAuthenticate 事件期间设置的用户名。

注意

如果 Web 应用程序在集成模式下的 IIS 7.0 中运行, Authenticate 则不会引发 的事件 DefaultAuthenticationModulemode如果身份验证配置元素的 属性设置为“None”,并且应用程序订阅事件Authenticate,则会引发错误PlatformNotSupportedException。 在此方案中,若要接收身份验证通知,请 AuthenticateRequest 订阅 实例的事件 HttpApplication 。 有关集成模式下兼容性问题的详细信息,请参阅 将 ASP.NET 应用程序从 IIS 6.0 移动到 IIS 7.0

适用于

另请参阅