FormsAuthenticationModule 類別

定義

在啟用表單驗證時,設定 ASP.NET 應用程式的使用者識別。 此類別無法獲得繼承。

public ref class FormsAuthenticationModule sealed : System::Web::IHttpModule
public sealed class FormsAuthenticationModule : System.Web.IHttpModule
type FormsAuthenticationModule = class
    interface IHttpModule
Public NotInheritable Class FormsAuthenticationModule
Implements IHttpModule
繼承
FormsAuthenticationModule
實作

範例

下列範例會使用FormsAuthentication_OnAuthenticate事件,將目前 HttpContext 的 屬性設定 User 為自訂 IPrincipal 物件。

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 目前的 HttpContext.User 屬性設定為 IPrincipal 物件,此物件表示在驗證元素中設定 Forms 為 的驗證Mode 專案 (ASP.NET 設定架構) 應用程式組態檔中的目前要求的使用者身分識別。

FormsAuthenticationModuleAuthenticate 公開事件,讓您為目前 HttpContext 的 屬性提供自訂 IPrincipal 物件 User 。 在 Authenticate ASP.NET 應用程式的 Global.asax 檔案中指定名為 FormsAuthentication_OnAuthenticate 的副程式,即可存取此事件。

注意

在 IIS 7.0 中,不支援搭配 使用 WindowsAuthenticationModuleFormsAuthenticationModule 。 如需整合模式中相容性問題的詳細資訊,請參閱 將 ASP.NET 應用程式從 IIS 6.0 移至 IIS 7.0

建構函式

FormsAuthenticationModule()

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

方法

Dispose()

除了記憶體之外,釋放 FormsAuthenticationModule 使用的所有資源。

Equals(Object)

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

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
Init(HttpApplication)

初始化 FormsAuthenticationModule 物件。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

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

(繼承來源 Object)

事件

Authenticate

在應用程式驗證目前要求時發生。

適用於

另請參閱