DefaultAuthenticationModule 類別

定義

確保在上下文中存在認證物件。 此類別無法獲得繼承。

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

範例

以下範例利用DefaultAuthentication_OnAuthenticate事件來測試目前User實例的性質是否HttpContextnull。 若User屬性為 null,則範例將當前User實例HttpContext的屬性設GenericPrincipal為一個物件,其中物件的 為 IdentityGenericPrincipal aGenericIdentity,值為Name「default」。

Note

DefaultAuthentication_OnAuthenticate事件會在活動前AuthorizeRequest提出。 因此,如果你將目前User實例的屬性設定HttpContext為自訂身份,可能會影響應用程式的行為。 舉例來說,如果你使用 這個FormsAuthentication類別,並在<deny users="?" />設定區指定只有已認證的使用者才能存取你的網站,這個範例會忽略拒絕元素,因為使用者會有一個「預設」的名稱。相反地,你應該指定<deny users="default" />只有經過認證的使用者才能存取你的網站。

public void DefaultAuthentication_OnAuthenticate(object sender,
                                                 DefaultAuthenticationEventArgs args)
{
  if (args.Context.User == null)
    args.Context.User = 
      new System.Security.Principal.GenericPrincipal(
        new System.Security.Principal.GenericIdentity("default"),
        new String[0]);
}
Public Sub DefaultAuthentication_OnAuthenticate(sender As Object, _
                                                args As DefaultAuthenticationEventArgs)
  If args.Context.User Is Nothing Then
    args.Context.User = _
      new System.Security.Principal.GenericPrincipal( _
        new System.Security.Principal.GenericIdentity("default"), _
        new String(0) {})
  End If
End Sub

備註

確保DefaultAuthenticationModuleUser目前HttpContext實例的屬性為每個請求都設定為物件IPrincipal。 他們DefaultAuthenticationModule會在User事件AuthenticateRequest發生前後AuthorizeRequest檢查該物業。 若屬性Usernull,則該DefaultAuthenticationModuleUser屬性GenericPrincipal設定為不包含使用者資訊的物件。

若認證模組將屬性設 StatusCode 為 401,則 DefaultAuthenticationModule 會顯示一個存取被拒絕的錯誤頁面。 如果屬性的值 StatusCode 設定為大於 200, DefaultAuthenticationModule 物件就會終止請求。 在這種情況下,只有訂閱事件的 HTTP 模組 EndRequest 會在目前請求完成前被呼叫。

DefaultAuthenticationModule 露事件 Authenticate 。 你可以利用這個事件為目前IPrincipal實例的屬性提供自訂User物件HttpContextAuthenticate透過在應用程式的 Global.asax 檔案中指定名為 DefaultAuthentication_OnAuthenticate 的事件來存取該事件。

建構函式

名稱 Description
DefaultAuthenticationModule()

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

方法

名稱 Description
Dispose()

釋放除記憶體外的所有資源 DefaultAuthenticationModule

Equals(Object)

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

(繼承來源 Object)
GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetType()

取得目前實例的 Type

(繼承來源 Object)
Init(HttpApplication)

初始化 DefaultAuthenticationModule 物件。

MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ToString()

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

(繼承來源 Object)

事件

名稱 Description
Authenticate

發生在請求驗證後。

適用於

另請參閱