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 引發。

您可以在 AuthenticateFormsAuthenticationModule ASP.NET 應用程式的 Global.asax 檔案中指定名為 FormsAuthentication_OnAuthenticate 的副程式,來處理 類別的事件。

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

只有在驗證模式設定 Forms 為 的驗證元素 (ASP.NET 設定架構) 應用程式組態檔的 元素中,而且 FormsAuthenticationModule 是應用程式的使用中 HTTP 模組時,才會引發FormsAuthentication_OnAuthenticate事件。

適用於

另請參閱