ISessionIDManager インターフェイス

定義

カスタムのセッション状態識別子マネージャーが実装する必要があるコントラクトを定義します。

public interface class ISessionIDManager
public interface ISessionIDManager
type ISessionIDManager = interface
Public Interface ISessionIDManager
派生

次のコード例は、Cookie ベースのセッション ID マネージャーを実装するクラスを示しています。

using System;
using System.Configuration;
using System.Web.Configuration;
using System.Web;
using System.Web.SessionState;

namespace Samples.AspNet.Session
{

  public class MySessionIDManager : IHttpModule, ISessionIDManager
  {

    private SessionStateSection pConfig = null;

    //
    // IHttpModule Members
    //

    //
    // IHttpModule.Init
    //

    public void Init(HttpApplication app)
    {
      // Obtain session-state configuration settings.

      if (pConfig == null)
      {
        Configuration cfg =
          WebConfigurationManager.OpenWebConfiguration(System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath);
        pConfig = (SessionStateSection)cfg.GetSection("system.web/sessionState");
      }
    }

    //
    // IHttpModule.Dispose
    //

    public void Dispose()
    {
    }

    //
    // ISessionIDManager Members
    //

    //
    // ISessionIDManager.Initialize
    //

    public void Initialize()
    {
    }

    //
    // ISessionIDManager.InitializeRequest
    //

    public bool InitializeRequest(HttpContext context, 
                                  bool suppressAutoDetectRedirect, 
                                  out bool supportSessionIDReissue)
    {
      if (pConfig.Cookieless == HttpCookieMode.UseCookies)
      {
        supportSessionIDReissue = false;
        return false;
      }
      else
      {
        supportSessionIDReissue = true;
        return context.Response.IsRequestBeingRedirected;
      }
    }

    //
    // ISessionIDManager.GetSessionID
    //
    public string GetSessionID(HttpContext context)
    {
      string id = null;

      if (pConfig.Cookieless == HttpCookieMode.UseUri)
      {
        // Retrieve the SessionID from the URI.
      }
      else
      {
        id = context.Request.Cookies[pConfig.CookieName].Value;
      }      

      // Verify that the retrieved SessionID is valid. If not, return null.

      if (!Validate(id))
        id = null;

      return id;
    }

    //
    // ISessionIDManager.CreateSessionID
    //

    public string CreateSessionID(HttpContext context)
    {
      return Guid.NewGuid().ToString();
    }

    //
    // ISessionIDManager.RemoveSessionID
    //

    public void RemoveSessionID(HttpContext context)
    {
      context.Response.Cookies.Remove(pConfig.CookieName);
    }

    //
    // ISessionIDManager.SaveSessionID
    //

    public void SaveSessionID(HttpContext context, string id, out bool redirected, out bool cookieAdded)
    {
      redirected = false;
      cookieAdded = false;

      if (pConfig.Cookieless == HttpCookieMode.UseUri)
      {
        // Add the SessionID to the URI. Set the redirected variable as appropriate.

        redirected = true;
        return;
      }
      else
      {
        context.Response.Cookies.Add(new HttpCookie(pConfig.CookieName, id));
        cookieAdded = true;
      }
    }

    //
    // ISessionIDManager.Validate
    //

    public bool Validate(string id)
    {
      try
      {
        Guid testGuid = new Guid(id);

        if (id == testGuid.ToString())
          return true;
      }
      catch
      {
      }

      return false;
    }
  }
}
Imports System.Configuration
Imports System.Web.Configuration
Imports System.Web
Imports System.Web.SessionState


Namespace Samples.AspNet.Session


  Public Class MySessionIDManager
    Implements IHttpModule, ISessionIDManager

    Private pConfig As SessionStateSection = Nothing

    '
    ' IHttpModule Members
    '

    '
    ' IHttpModule.Init
    '

    Public Sub Init(app As HttpApplication) Implements IHttpModule.Init
    
      ' Obtain session-state configuration settings.

      If pConfig Is Nothing Then      
        Dim cfg As System.Configuration.Configuration = _
          WebConfigurationManager.OpenWebConfiguration( _
            System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath)
        pConfig = CType(cfg.GetSection("system.web/sessionState"), SessionStateSection)
      End If

    End Sub


    '
    ' IHttpModule.Dispose
    '

    Public Sub Dispose() Implements IHttpModule.Dispose
    
    End Sub



    '
    ' ISessionIDManager.Initialize
    '

    Public Sub Initialize() Implements ISessionIDManager.Initialize

    End Sub


    '
    ' ISessionIDManager.InitializeRequest
    '

    Public Function InitializeRequest(context As HttpContext,  _
                                      suppressAutoDetectRedirect As Boolean,  _
                                      ByRef supportSessionIDReissue As Boolean) As Boolean _
                                      Implements ISessionIDManager.InitializeRequest

      If pConfig.Cookieless = HttpCookieMode.UseCookies Then
        supportSessionIDReissue = False
        Return False
      Else
        supportSessionIDReissue = True
        Return context.Response.IsRequestBeingRedirected
      End If
    End Function



    '
    ' ISessionIDManager Members
    '


    '
    ' ISessionIDManager.GetSessionID
    '
    Public Function GetSessionID(context As HttpContext) As String _
      Implements ISessionIDManager.GetSessionID
    
      Dim id As String = Nothing

      If pConfig.Cookieless = HttpCookieMode.UseUri Then
        ' Retrieve the SessionID from the URI.
      Else
        id = context.Request.Cookies(pConfig.CookieName).Value
      End If    

      ' Verify that the retrieved SessionID is valid. If not, return Nothing.

      If Not Validate(id) Then _
        id = Nothing

      Return id
    End Function

    '
    ' ISessionIDManager.CreateSessionID
    '

    Public Function CreateSessionID(context As HttpContext) As String _
      Implements ISessionIDManager.CreateSessionID
    
      Return Guid.NewGuid().ToString()
    End Function

    '
    ' ISessionIDManager.RemoveSessionID
    '

    Public Sub RemoveSessionID(context As HttpContext) _
      Implements ISessionIDManager.RemoveSessionID

      context.Response.Cookies.Remove(pConfig.CookieName)

    End Sub


    '
    ' ISessionIDManager.SaveSessionID
    '

    Public Sub SaveSessionID(context As HttpContext, _
                             id As String, _
                             ByRef redirected As Boolean, _
                             ByRef cookieAdded As Boolean) _
      Implements ISessionIDManager.SaveSessionID
    
      redirected = False
      cookieAdded = False

      If pConfig.Cookieless = HttpCookieMode.UseUri Then

        ' Add the SessionID to the URI. Set the redirected variable as appropriate.

        redirected = True
        Return
      Else
        context.Response.Cookies.Add(New HttpCookie(pConfig.CookieName, id))
        cookieAdded = True
      End If
    End Sub


    '
    ' ISessionIDManager.Validate
    '

    Public Function Validate(id As String) As Boolean _
      Implements ISessionIDManager.Validate
    
      Try
        Dim testGuid As Guid = New Guid(id)

        If id = testGuid.ToString() Then _
          Return True
      Catch
      
      End Try

      Return False
    End Function

  End Class
End Namespace

注釈

インターフェイスは ISessionIDManager 、セッション識別子の値のカスタム マネージャーを作成するために実装する必要があるメソッドを識別します。 インターフェイスの ISessionIDManager 実装では、セッション識別子の値を作成して検証し、HTTP 応答でのセッション識別子の格納と、HTTP 要求からのセッション識別子値の取得を管理します。 sessionState 要素 (ASP.NET 設定 Schema) 構成要素の属性を使用してsessionIDManagerType、カスタム セッション ID マネージャーを有効にします。

インターフェイスの ISessionIDManager 実装で Cookie レス セッション識別子がサポートされる場合は、ISAPI フィルターなどの URL でセッション識別子を送信および取得するためのソリューションを実装する必要があります。

セッション状態で使用するカスタム セッション識別子の値のみを指定する場合は、クラス ASP.NET 継承SessionIDManagerするクラスを作成し、独自のカスタム実装でCreateSessionIDメソッドとValidateメソッドのみをオーバーライドできます。 これにより、HTTP 応答に値を格納し、HTTP 要求から値を取得するために基底 SessionIDManager クラスに依存しながら、独自のセッション識別子の値を指定できます。 クラスをオーバーライドし、これらのメソッドを SessionIDManager 実装する例については、クラスのメソッドに対して CreateSessionID 提供される例を SessionIDManager 参照してください。

メソッド

CreateSessionID(HttpContext)

一意のセッション識別子を作成します。

GetSessionID(HttpContext)

現在の HTTP 要求のコンテキストからセッション識別子を取得します。

Initialize()

SessionIDManager オブジェクトを初期化します。

InitializeRequest(HttpContext, Boolean, Boolean)

SessionIDManager オブジェクトの要求ごとの初期化を実行します。

RemoveSessionID(HttpContext)

クッキーまたは URL からセッション識別子を削除します。

SaveSessionID(HttpContext, String, Boolean, Boolean)

新規作成されたセッション識別子を HTTP 応答に保存します。

Validate(String)

提供されたセッション識別子が有効であることを確認します。

適用対象

こちらもご覧ください