FormsAuthenticationEventHandler 委托

定义

表示处理 FormsAuthenticationModuleFormsAuthentication_OnAuthenticate 事件的方法。Represents the method that handles the FormsAuthentication_OnAuthenticate event of a FormsAuthenticationModule.

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

参数

sender
Object

事件源。The source of the event.

e
FormsAuthenticationEventArgs

包含事件数据的 FormsAuthenticationEventArgsA FormsAuthenticationEventArgs that contains the event data.

继承
FormsAuthenticationEventHandler

示例

下面的代码示例使用FormsAuthentication_OnAuthenticate事件User将的HttpContext属性GenericPrincipal设置为具有自定义Identity的对象。The following code example uses the FormsAuthentication_OnAuthenticate event to set the User property of the current HttpContext to a GenericPrincipal object with a custom 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

注解

委托是FormsAuthenticationModule Authenticate为类的事件定义的。 FormsAuthenticationEventHandlerThe FormsAuthenticationEventHandler delegate is defined for the Authenticate event of the FormsAuthenticationModule class. 可以通过在 ASP.NET Authenticate应用程序的FormsAuthenticationModule global.asax 文件中指定名为FormsAuthentication_OnAuthenticate的子例程来访问类的事件。You can access the Authenticate event of the FormsAuthenticationModule class by specifying a subroutine named FormsAuthentication_OnAuthenticate in the Global.asax file for your ASP.NET application. Authenticate 事件AuthenticateRequest在事件期间引发。The Authenticate event is raised during the AuthenticateRequest event.

使用当前FormsAuthenticationModule FormsAuthenticationEventArgs 构造对象并将其传递给FormsAuthentication_OnAuthenticate事件。HttpContextThe FormsAuthenticationModule constructs a FormsAuthenticationEventArgs object using the current HttpContext and passes it to the FormsAuthentication_OnAuthenticate event.

可以使用提供给User FormsAuthentication_OnAuthenticate事件UserFormsAuthenticationEventArgs对象的属性, 将当前HttpContext的属性设置为自定义IPrincipal对象。You can use the User property of the FormsAuthenticationEventArgs object supplied to the FormsAuthentication_OnAuthenticate event to set the User property of the current HttpContext to a custom IPrincipal object. 如果未在User FormsAuthentication_OnAuthenticate事件期间为属性指定值, 则使用 cookie 或 URL 中的 forms 身份验证票证提供的标识。If you do not specify a value for the User property during the FormsAuthentication_OnAuthenticate event, the identity supplied by the forms authentication ticket in the cookie or URL is used.

仅当身份验证Mode Forms设置为, 并且是应用程序的活动HTTP模块时,才会引发FormsAuthentication_OnAuthenticateFormsAuthenticationModule事件。The FormsAuthentication_OnAuthenticate event is only raised when the authentication Mode is set to Forms and the FormsAuthenticationModule is an active HTTP module for the application.

扩展方法

GetMethodInfo(Delegate)

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

适用于

另请参阅