DefaultAuthenticationModule 類別

定義

確定內容中有驗證 (Authentication) 物件。 此類別無法獲得繼承。

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事件來測試目前 HttpContext 實例的 屬性是否 UsernullUser如果屬性為 null ,則範例會將 User 目前 HttpContext 實例的 屬性設定為 GenericPrincipal 物件,其中 IdentityGenericPrincipal 物件的 是 GenericIdentity 值為 Name 「default」 的 。

注意

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

備註

DefaultAuthenticationModule可確保 User 目前 HttpContext 實例的 屬性設定 IPrincipal 為每個要求的物件。 會 DefaultAuthenticationModule 檢查 User 事件之後 AuthenticateRequest 和事件之前 AuthorizeRequest 的屬性。 User如果 屬性為 null ,會將 DefaultAuthenticationModule 屬性設定 UserGenericPrincipal 不包含任何使用者資訊的 物件。

如果驗證模組將 StatusCode 屬性設定為 401,則會 DefaultAuthenticationModule 呈現拒絕存取的錯誤頁面。 如果 屬性的值 StatusCode 設定為大於 200 的值,物件 DefaultAuthenticationModule 將會結束要求。 在此情況下,在完成目前要求之前,只會呼叫訂閱 EndRequest 事件的 HTTP 模組。

DefaultAuthenticationModule 公開 Authenticate 事件。 您可以使用這個事件,為 User 目前 HttpContext 實例的 屬性提供自訂 IPrincipal 物件。 在應用程式的 Global.asax 檔案中指定名為DefaultAuthentication_OnAuthenticate的事件,即可 Authenticate 存取此事件。

建構函式

DefaultAuthenticationModule()

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

方法

Dispose()

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

Equals(Object)

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

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
Init(HttpApplication)

初始化 DefaultAuthenticationModule 物件。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

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

(繼承來源 Object)

事件

Authenticate

在要求已經驗證之後發生。

適用於

另請參閱