FormsAuthenticationModule.Authenticate 事件

定义

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

public event System.Web.Security.FormsAuthenticationEventHandler Authenticate;

事件类型

示例

下面的代码示例使用 FormsAuthentication_OnAuthenticate 事件将当前 HttpContextGenericPrincipal 的 属性设置为User具有自定义 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.");
  }
}

注解

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

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

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

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

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

另请参阅