FormsAuthenticationModule.Authenticate 事件

定义

应用程序对当前请求进行身份验证时发生。

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

事件类型

示例

下面的代码示例使用 FormsAuthentication_OnAuthenticate 事件将当前 HttpContext 的 属性设置为UserGenericPrincipal具有自定义 Identity的对象。

public void FormsAuthentication_OnAuthenticate(object sender, FormsAuthenticationEventArgs args)
{
  if (FormsAuthentication.CookiesSupported)
  {
    if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
    {
      try
      {
        FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(
          Request.Cookies[FormsAuthentication.FormsCookieName].Value);
        
        args.User = new System.Security.Principal.GenericPrincipal(
          new Samples.AspNet.Security.MyFormsIdentity(ticket),
          new string[0]);
      }
      catch (Exception e)
      {
        // Decrypt method failed.
      }
    }
  }
  else
  {
    throw new HttpException("Cookieless Forms Authentication is not " +
                            "supported for this application.");
  }
}
Public Sub FormsAuthentication_OnAuthenticate(sender As Object, _
                                              args As FormsAuthenticationEventArgs)
  If FormsAuthentication.CookiesSupported Then
    If Not Request.Cookies(FormsAuthentication.FormsCookieName) Is Nothing Then
      Try
        Dim ticket As FormsAuthenticationTicket = FormsAuthentication.Decrypt( _
          Request.Cookies(FormsAuthentication.FormsCookieName).Value)
        
        args.User = New System.Security.Principal.GenericPrincipal( _
          New Samples.AspNet.Security.MyFormsIdentity(ticket), _
          New String(0) {})
      Catch e As HttpException
        ' Decrypt method failed.
      End Try
    End If
  Else
      Throw New Exception("Cookieless Forms Authentication is not " & _
                            "supported for this application.")
  End If
End Sub

注解

事件 Authenticate 在事件期间 AuthenticateRequest 引发。

可以通过AuthenticateGlobal.asax 文件中为 ASP.NET 应用程序指定名为 FormsAuthentication_OnAuthenticate 的子例程来处理 类的事件FormsAuthenticationModule

可以使用FormsAuthenticationEventArgsUser提供给 FormsAuthentication_OnAuthenticate 事件的属性将当前 HttpContext 对象的 属性设置为User自定义IPrincipal对象。 如果在FormsAuthentication_OnAuthenticate事件期间未为 User 属性指定值,则使用 cookie 或 URL 中的表单身份验证票证提供的标识。

仅当身份验证模式在应用程序的配置文件的 authentication 元素 (ASP.NET“设置”架构) 元素中将身份验证模式设置为 Forms ,并且 是应用程序的活动 HTTP 模块时,FormsAuthenticationModule才会引发 FormsAuthentication_OnAuthenticate 事件。

适用于

另请参阅