FormsAuthenticationEventHandler 委托

定义

表示处理 FormsAuthenticationModuleFormsAuthentication_OnAuthenticate 事件的方法。

C#
public delegate void FormsAuthenticationEventHandler(object sender, FormsAuthenticationEventArgs e);

参数

sender
Object

事件源。

示例

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

C#
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.");
  }
}

注解

委托 FormsAuthenticationEventHandler 是为 Authenticate 类的 FormsAuthenticationModule 事件定义的。 可以通过在 global.asax 文件中为 ASP.NET 应用程序指定名为 FormsAuthentication_OnAuthenticate 的子例程来访问 Authenticate 类的 事件。FormsAuthenticationModule 事件 Authenticate 在 事件期间 AuthenticateRequest 引发。

FormsAuthenticationModule使用当前 HttpContext 构造 FormsAuthenticationEventArgs 对象,并将其传递给 FormsAuthentication_OnAuthenticate 事件。

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

仅当身份验证Mode设置为 Forms 并且 是应用程序的活动 HTTP 模块时,FormsAuthenticationModule才会引发 FormsAuthentication_OnAuthenticate 事件。

扩展方法

GetMethodInfo(Delegate)

获取指示指定委托表示的方法的对象。

适用于

产品 版本
.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

另请参阅