DefaultAuthenticationModule.Authenticate 事件
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在要求已經驗證之後發生。
public:
event System::Web::Security::DefaultAuthenticationEventHandler ^ Authenticate;
public event System.Web.Security.DefaultAuthenticationEventHandler Authenticate;
member this.Authenticate : System.Web.Security.DefaultAuthenticationEventHandler
Public Custom Event Authenticate As DefaultAuthenticationEventHandler
事件類型
範例
下列程式碼範例會使用DefaultAuthentication_OnAuthenticate事件來測試目前 HttpContext 實例的 屬性是否 User 為 null
。 User如果屬性為 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
備註
事件 Authenticate 會在 事件之後 AuthenticateRequest 引發。 它用來確保 User 目前 HttpContext 實例的 屬性已填入 IPrincipal 物件。
您可以在應用程式的 Global.asax 檔案中指定名為DefaultAuthentication_OnAuthenticate的副程式,以存取 AuthenticateDefaultAuthenticationModule 類別的事件。
您可以在DefaultAuthentication_OnAuthenticate事件中使用 Context 物件的 屬性 DefaultAuthenticationEventArgs ,將目前 HttpContext 實例的 屬性設定 User 為自訂 IPrincipal 物件。 如果您未指定 屬性的值 User ,會將 DefaultAuthenticationModule 實例的 HttpContext 屬性設定 User 為 GenericPrincipal 不包含任何使用者資訊的 物件。
DefaultAuthentication_OnAuthenticate事件會在 事件之後 AuthenticateRequest 和 事件之前 AuthorizeRequest 引發。 如果您有一個 authorization
區段相依于要拒絕或允許存取您的應用程式的使用者名稱,修改 User 目前 HttpContext 實例的 屬性可能會影響應用程式的行為。 當您在組態中指定授權區段時,請務必考慮您在DefaultAuthentication_OnAuthenticate事件期間設定的使用者名稱。
注意
如果 Web 應用程式是以整合模式在 IIS 7.0 中執行, Authenticate 則 不會引發 的 DefaultAuthenticationModule 事件。 mode
如果驗證組態專案的 屬性設定為 「None」,且應用程式訂閱 Authenticate 事件, PlatformNotSupportedException 就會引發錯誤。 在此案例中,若要接收驗證通知,請訂閱 AuthenticateRequest 實例的事件 HttpApplication 。 如需整合模式中相容性問題的詳細資訊,請參閱 將 ASP.NET 應用程式從 IIS 6.0 移至 IIS 7.0。
適用於
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應