DefaultAuthenticationEventHandler 代理人

定義

表示處理 DefaultAuthenticationModuleDefaultAuthentication_OnAuthenticate 事件的方法。

public delegate void DefaultAuthenticationEventHandler(System::Object ^ sender, DefaultAuthenticationEventArgs ^ e);
public delegate void DefaultAuthenticationEventHandler(object sender, DefaultAuthenticationEventArgs e);
type DefaultAuthenticationEventHandler = delegate of obj * DefaultAuthenticationEventArgs -> unit
Public Delegate Sub DefaultAuthenticationEventHandler(sender As Object, e As DefaultAuthenticationEventArgs)

參數

sender
Object

事件的來源。

範例

下列程式碼範例會使用DefaultAuthentication_OnAuthenticate事件來測試目前 HttpContext 的 屬性是否 Usernull 。 如果屬性為 null ,則範例會將 User 目前 HttpContext 的 屬性設定為 GenericPrincipal 物件,其中 IdentityGenericPrincipal 物件的 是 GenericIdentity 值為 Name 「default」 的 。

注意

DefaultAuthentication_OnAuthenticate 事件會在 事件之前 AuthorizeRequest 引發。 因此,如果您將 User 目前 HttpContext 的 屬性設定為自訂身分識別,可能會影響應用程式的行為。 例如,如果您使用 類別 FormsAuthentication ,而且您確定只有已驗證的使用者能夠存取您的網站,方法是使用 authorization 區段並指定 <deny users="?" /> ,這個範例會導致 deny 忽略 元素,因為使用者會有名稱,也就是 「預設值」。 相反地,您可以指定 <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

備註

委派 DefaultAuthenticationEventHandler 是針對 Authenticate 類別的 DefaultAuthenticationModule 事件所定義。 您可以在 ASP.NET 應用程式的 Global.asax 檔案中指定名為DefaultAuthentication_OnAuthenticate的副程式,以存取 AuthenticateDefaultAuthenticationModule 類別的事件。 事件 Authenticate 會在 事件之後 AuthenticateRequest 引發,並用來確保 User 目前 HttpContext 的 屬性會 IPrincipal 填入 物件。

您可以使用 Context 提供給DefaultAuthentication_OnAuthenticate事件之 DefaultAuthenticationEventArgs 物件的 屬性,將目前 HttpContext 的 屬性設定 User 為自訂 IPrincipal 物件。 如果您未指定DefaultAuthentication_OnAuthenticate事件期間所提供 之 屬性 HttpContext 的值 User ,會將 DefaultAuthenticationModuleHttpContext 屬性設定 UserGenericPrincipal 不包含任何使用者資訊的 物件。

DefaultAuthentication_OnAuthenticate事件會在 事件之後 AuthenticateRequest 和 事件之前 AuthorizeRequest 引發。 如果您有一個 authorization 區段相依于使用者名稱來拒絕或允許存取您的應用程式,則修改 User 目前 HttpContext 的 屬性可能會影響應用程式的行為。 當您在 態中指定 authorization 區段時,請務必考慮您在DefaultAuthentication_OnAuthenticate事件期間設定的使用者名稱。

擴充方法

GetMethodInfo(Delegate)

取得表示特定委派所代表之方法的物件。

適用於