FormsAuthenticationEventArgs 類別

定義

提供資料給 FormsAuthentication_OnAuthenticate 事件。 此類別無法獲得繼承。

public ref class FormsAuthenticationEventArgs sealed : EventArgs
public sealed class FormsAuthenticationEventArgs : EventArgs
type FormsAuthenticationEventArgs = class
    inherit EventArgs
Public NotInheritable Class FormsAuthenticationEventArgs
Inherits EventArgs
繼承
FormsAuthenticationEventArgs

範例

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

備註

類別 FormsAuthenticationEventArgs 可用來提供事件值給 FormsAuthentication_OnAuthenticate 事件,並可讓您指定目前要求的自訂身分識別。

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

您可以使用 User 屬性,將目前 HttpContext 的 屬性設定 User 為自訂 IPrincipal 物件。

如果您在FormsAuthentication_OnAuthenticate事件期間未指定 屬性的值 User ,則會使用 Cookie 或 URL 中表單驗證票證所提供的身分識別。

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

建構函式

FormsAuthenticationEventArgs(HttpContext)

初始化 FormsAuthenticationEventArgs 類別的新執行個體。

屬性

Context

取得目前 HTTP 要求的 HttpContext 物件。

User

取得或設定要與目前要求相關聯的 IPrincipal 物件。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

另請參閱