次の方法で共有


FormsAuthenticationEventHandler デリゲート

FormsAuthenticationModuleFormsAuthentication_OnAuthenticate イベントを処理するメソッドを表します。

名前空間: System.Web.Security
アセンブリ: System.Web (system.web.dll 内)

構文

'宣言
Public Delegate Sub FormsAuthenticationEventHandler ( _
    sender As Object, _
    e As FormsAuthenticationEventArgs _
)
'使用
Dim instance As New FormsAuthenticationEventHandler(AddressOf HandlerMethod)
public delegate void FormsAuthenticationEventHandler (
    Object sender,
    FormsAuthenticationEventArgs e
)
public delegate void FormsAuthenticationEventHandler (
    Object^ sender, 
    FormsAuthenticationEventArgs^ e
)
/** @delegate */
public delegate void FormsAuthenticationEventHandler (
    Object sender, 
    FormsAuthenticationEventArgs e
)
適用できません。

パラメータ

  • sender
    イベントのソース。

解説

FormsAuthenticationEventHandler デリゲートは、FormsAuthenticationModule クラスの Authenticate イベントに対して定義されています。FormsAuthenticationModule クラスの Authenticate イベントには、ASP.NET アプリケーションの Global.asax ファイルに、FormsAuthentication_OnAuthenticate というサブルーチンを指定することによってアクセスできます。Authenticate イベントは、AuthenticateRequest イベント時に生成されます。

FormsAuthenticationModule は、現在の HttpContext を使用して FormsAuthenticationEventArgs オブジェクトを生成し、それを FormsAuthentication_OnAuthenticate イベントに渡します。

FormsAuthentication_OnAuthenticate イベントに渡した FormsAuthenticationEventArgs オブジェクトの User プロパティを使用して、現在の HttpContextUser プロパティを、カスタムの IPrincipal オブジェクトに設定できます。FormsAuthentication_OnAuthenticate イベント中に User プロパティ値を指定しない場合、Cookie または URL 内のフォーム認証チケットによって提供される ID が使用されます。

FormsAuthentication_OnAuthenticate イベントは、認証の ModeForms に設定され、FormsAuthenticationModule がアプリケーションのアクティブ HTTP モジュールである場合にのみ生成されます。

使用例

FormsAuthentication_OnAuthenticate イベントを使用して、現在の HttpContextUser プロパティにカスタム Identity を持つ GenericPrincipal オブジェクトを設定するコード例を次に示します。

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
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.");
  }
}

プラットフォーム

Windows 98,Windows Server 2000 SP4,Windows CE,Windows Millennium Edition,Windows Mobile for Pocket PC,Windows Mobile for Smartphone,Windows Server 2003,Windows XP Media Center Edition,Windows XP Professional x64 Edition,Windows XP SP2,Windows XP Starter Edition

Microsoft .NET Framework 3.0 は Windows Vista,Microsoft Windows XP SP2,および Windows Server 2003 SP1 でサポートされています。

バージョン情報

.NET Framework

サポート対象 : 3.0,2.0,1.1,1.0

参照

関連項目

System.Web.Security 名前空間

その他の技術情報

フォーム認証プロバイダ