FormsAuthenticationEventHandler 代理人

定義

表示處理 FormsAuthenticationModuleFormsAuthentication_OnAuthenticate 事件的方法。

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

事件的來源。

範例

下列程式碼範例會使用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

備註

委派 FormsAuthenticationEventHandler 是針對 Authenticate 類別的 FormsAuthenticationModule 事件所定義。 您可以在 ASP.NET 應用程式的 Global.asax 檔案中指定名為FormsAuthentication_OnAuthenticate的副程式,以存取 AuthenticateFormsAuthenticationModule 類別的事件。 事件 Authenticate 會在事件期間 AuthenticateRequest 引發。

FormsAuthenticationModule 使用目前的 HttpContext 建構 FormsAuthenticationEventArgs 物件,並將它傳遞給FormsAuthentication_OnAuthenticate事件。

您可以使用 User 提供給FormsAuthentication_OnAuthenticate事件之 FormsAuthenticationEventArgs 物件的 屬性,將目前 HttpContext 的 屬性設定 User 為自訂 IPrincipal 物件。 如果您在FormsAuthentication_OnAuthenticate事件期間未指定 屬性的值 User ,則會使用 Cookie 或 URL 中表單驗證票證所提供的身分識別。

只有在驗證 Mode 設定為 Forms ,而且 FormsAuthenticationModule 是應用程式的使用中 HTTP 模組時,才會引發FormsAuthentication_OnAuthenticate事件。

擴充方法

GetMethodInfo(Delegate)

取得表示特定委派所代表之方法的物件。

適用於

另請參閱