IHttpSessionState IHttpSessionState IHttpSessionState IHttpSessionState Interface

定義

カスタムのセッション状態コンテナーを実装するコントラクトを定義します。Defines the contract to implement a custom session-state container.

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

次のコード例の実装、IHttpSessionStateという名前の新しいセッション状態コンテナー クラスを作成するインターフェイスMySessionStateします。The following code example implements the IHttpSessionState interface to create a new session-state container class named MySessionState.

using System;
using System.Web;
using System.Web.SessionState;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
using System.Threading;
using System.Globalization;

namespace Samples.AspNet.SessionState
{
  public sealed class MySessionState : IHttpSessionState
  {
    const int MAX_TIMEOUT = 24 * 60;  // Timeout cannot exceed 24 hours.

    string                      pId;
    ISessionStateItemCollection pSessionItems;
    HttpStaticObjectsCollection pStaticObjects;
    int                         pTimeout;
    bool                        pNewSession;
    HttpCookieMode              pCookieMode;
    SessionStateMode            pMode;
    bool                        pAbandon;
    bool                        pIsReadonly;

    public MySessionState(string                      id, 
                          ISessionStateItemCollection sessionItems,
                          HttpStaticObjectsCollection staticObjects,
                          int                         timeout,
                          bool                        newSession,
                          HttpCookieMode              cookieMode,
                          SessionStateMode            mode,
                          bool                        isReadonly)
    {
      pId            = id;   
      pSessionItems  = sessionItems;
      pStaticObjects = staticObjects;
      pTimeout       = timeout;    
      pNewSession    = newSession; 
      pCookieMode    = cookieMode;
      pMode          = mode;
      pIsReadonly    = isReadonly;
    }


    public int Timeout
    {
      get { return pTimeout; }
      set
      {
        if (value <= 0)
          throw new ArgumentException("Timeout value must be greater than zero.");

        if (value > MAX_TIMEOUT)
          throw new ArgumentException("Timout cannot be greater than " + MAX_TIMEOUT.ToString());

        pTimeout = value;
      }
    }


    public string SessionID
    {
      get { return pId; }
    }


    public bool IsNewSession
    {
      get { return pNewSession; }
    }


    public SessionStateMode Mode
    {
      get { return pMode; }
    }


    public bool IsCookieless
    {
      get { return CookieMode == HttpCookieMode.UseUri; }
    }


    public HttpCookieMode CookieMode
    {
      get { return pCookieMode; }
    }


    //
    // Abandon marks the session as abandoned. The IsAbandoned property is used by the
    // session state module to perform the abandon work during the ReleaseRequestState event.
    //
    public void Abandon()
    {
      pAbandon = true;
    }

    public bool IsAbandoned
    {
      get { return pAbandon; }
    }

    //
    // Session.LCID exists only to support legacy ASP compatibility. ASP.NET developers should use
    // Page.LCID instead.
    //
    public int LCID
    {
      get { return Thread.CurrentThread.CurrentCulture.LCID; }
      set { Thread.CurrentThread.CurrentCulture = CultureInfo.ReadOnly(new CultureInfo(value)); }
    }


    //
    // Session.CodePage exists only to support legacy ASP compatibility. ASP.NET developers should use
    // Response.ContentEncoding instead.
    //
    public int CodePage
    {
      get
      { 
        if (HttpContext.Current != null)
          return HttpContext.Current.Response.ContentEncoding.CodePage;
        else
          return Encoding.Default.CodePage;
      }
      set
      { 
        if (HttpContext.Current != null)
          HttpContext.Current.Response.ContentEncoding = Encoding.GetEncoding(value);
      }
    }


    public HttpStaticObjectsCollection StaticObjects
    {
      get { return pStaticObjects; }
    }


    public object this[string name]
    {
      get { return pSessionItems[name]; }
      set { pSessionItems[name] = value; }
    }


    public object this[int index]
    {
      get { return pSessionItems[index]; }
      set { pSessionItems[index] = value; }
    }
        

    public void Add(string name, object value)
    {
      pSessionItems[name] = value;        
    }


    public void Remove(string name)
    {
      pSessionItems.Remove(name);
    }


    public void RemoveAt(int index)
    {
      pSessionItems.RemoveAt(index);
    }


    public void Clear()
    {
      pSessionItems.Clear();
    }

    public void RemoveAll()
    {
        Clear();
    }



    public int Count
    {
      get { return pSessionItems.Count; }
    }



    public NameObjectCollectionBase.KeysCollection Keys
    {
      get { return pSessionItems.Keys; }
    }


    public IEnumerator GetEnumerator()
    {
      return pSessionItems.GetEnumerator();
    }


    public void CopyTo(Array items, int index)
    {
      foreach (object o in items)
        items.SetValue(o, index++);
    }


    public object SyncRoot
    {
        get { return this; }
    }


    public bool IsReadOnly
    {
      get { return pIsReadonly; }
    }


    public bool IsSynchronized
    {
      get { return false; }
    }
  }
}
Imports System
Imports System.Web
Imports System.Web.SessionState
Imports System.Collections
Imports System.Collections.Specialized
Imports System.Text
Imports System.Threading
Imports System.Globalization

Namespace Samples.AspNet.SessionState

  Public NotInheritable Class MySessionState
    Implements IHttpSessionState
  
    Const MAX_TIMEOUT As Integer = 24 * 60  ' Timeout cannot exceed 24 hours.

    Dim pId            As String
    Dim pSessionItems  As ISessionStateItemCollection
    Dim pStaticObjects As HttpStaticObjectsCollection
    Dim pTimeout       As Integer
    Dim pNewSession    As Boolean
    Dim pCookieMode    As HttpCookieMode
    Dim pMode          As SessionStateMode
    Dim pAbandon       As Boolean
    Dim pIsReadonly    As Boolean

    Public Sub New(id            As String, _
                   sessionItems  As ISessionStateItemCollection, _
                   staticObjects As HttpStaticObjectsCollection, _
                   timeout       As Integer, _
                   newSession    As Boolean, _
                   cookieMode    As HttpCookieMode, _
                   mode          As SessionStateMode, _  
                   isReadonly As Boolean)
    
      pId            = id   
      pSessionItems  = sessionItems
      pStaticObjects = staticObjects
      pTimeout       = timeout    
      pNewSession    = newSession 
      pCookieMode    = cookieMode
      pMode          = mode
      pIsReadonly    = isReadonly
    End Sub


    Public Property Timeout As Integer Implements IHttpSessionState.Timeout
      Get
        Return pTimeout
      End Get
      Set
        If value <= 0 Then _
          Throw New ArgumentException("Timeout value must be greater than zero.")

        If value > MAX_TIMEOUT Then _
          Throw New ArgumentException("Timout cannot be greater than " & MAX_TIMEOUT.ToString())

        pTimeout = value
      End Set
    End Property


    Public ReadOnly Property SessionID As String Implements IHttpSessionState.SessionID
      Get
        Return pId
      End Get
    End Property


    Public ReadOnly Property IsNewSession As Boolean Implements IHttpSessionState.IsNewSession
      Get
        Return pNewSession
      End Get
    End Property


    Public ReadOnly Property Mode As SessionStateMode Implements IHttpSessionState.Mode    
      Get
        Return pMode
      End Get
    End Property


    Public ReadOnly Property IsCookieless As Boolean Implements IHttpSessionState.IsCookieLess    
      Get
        Return CookieMode = HttpCookieMode.UseUri
      End Get
    End Property


    Public ReadOnly Property CookieMode As HttpCookieMode Implements IHttpSessionState.CookieMode    
      Get
        Return pCookieMode
      End Get
    End Property


    '
    ' Abandon marks the session as abandoned. The IsAbandoned property is used by the
    ' session state module to perform the abandon work during the ReleaseRequestState event.
    '
    Public Sub Abandon() Implements IHttpSessionState.Abandon
      pAbandon = True
    End Sub

    Public ReadOnly Property IsAbandoned As Boolean  
      Get
        Return pAbandon
      End Get
    End Property

    '
        ' Session.LCID exists only to support legacy ASP compatibility. ASP.NET developers should use
    ' Page.LCID instead.
    '
    Public Property LCID As Integer Implements IHttpSessionState.LCID
      Get
        Return Thread.CurrentThread.CurrentCulture.LCID
      End Get
      Set
        Thread.CurrentThread.CurrentCulture = CultureInfo.ReadOnly(new CultureInfo(value))
      End Set
    End Property


    '
        ' Session.CodePage exists only to support legacy ASP compatibility. ASP.NET developers should use
    ' Response.ContentEncoding instead.
    '
    Public Property CodePage As Integer Implements IHttpSessionState.CodePage    
      Get
        If Not HttpContext.Current Is Nothing Then
          Return HttpContext.Current.Response.ContentEncoding.CodePage
        Else
          Return Encoding.Default.CodePage
        End If
      End Get
      Set       
        If Not HttpContext.Current Is Nothing Then _
          HttpContext.Current.Response.ContentEncoding = Encoding.GetEncoding(value)
      End Set
    End Property


    Public ReadOnly Property StaticObjects As HttpStaticObjectsCollection _
      Implements IHttpSessionState.StaticObjects
    
      Get
        Return pStaticObjects
      End Get
    End Property


    Public Property Item(name As String) As Object Implements IHttpSessionState.Item
      Get
        Return pSessionItems(name)
      End Get
      Set
        pSessionItems(name) = value
      End Set
    End Property


    Public Property Item(index As Integer) As Object Implements IHttpSessionState.Item    
      Get
        Return pSessionItems(index)
      End Get
      Set
        pSessionItems(index) = value
      End Set
    End Property
        

    Public Sub Add(name As String, value As Object) Implements IHttpSessionState.Add    
      pSessionItems(name) = value        
    End Sub


    Public Sub Remove(name As String) Implements IHttpSessionState.Remove    
      pSessionItems.Remove(name)
    End Sub


    Public Sub RemoveAt(index As Integer) Implements IHttpSessionState.RemoveAt    
      pSessionItems.RemoveAt(index)
    End Sub


    Public Sub Clear() Implements IHttpSessionState.Clear 
      pSessionItems.Clear()
    End Sub

    Public Sub RemoveAll() Implements IHttpSessionState.RemoveAll
        Clear()
    End Sub



    Public ReadOnly Property Count As Integer Implements IHttpSessionState.Count    
      Get
        Return pSessionItems.Count
      End Get
    End Property



    Public ReadOnly Property Keys As NameObjectCollectionBase.KeysCollection _
      Implements IHttpSessionState.Keys
    
      Get
        Return pSessionItems.Keys
      End Get
    End Property


    Public Function GetEnumerator() As IEnumerator Implements IHttpSessionState.GetEnumerator
        Return pSessionItems.GetEnumerator()
    End Function


    Public Sub CopyTo(items As Array, index As Integer) Implements IHttpSessionState.CopyTo    
      For Each o As Object In items
        items.SetValue(o, index)
        index += 1
      Next
    End Sub


    Public ReadOnly Property SyncRoot As Object Implements IHttpSessionState.SyncRoot    
        Get
          Return Me
       End Get
    End Property


    Public ReadOnly Property IsReadOnly As Boolean Implements IHttpSessionState.IsReadOnly    
      Get
        Return pIsReadonly
      End Get
    End Property


    Public ReadOnly Property IsSynchronized As Boolean Implements IHttpSessionState.IsSynchronized    
      Get
        Return False
      End Get
    End Property
  End Class
End Namespace

注釈

セッション状態コンテナーは、セッション状態の値と現在のセッションに関連する情報へのアクセスを提供します。A session-state container provides access to the session-state values and related information for the current session. セッション状態コンテナーに含まれるセッション情報がを通じてアプリケーション コードに公開されている、HttpSessionStateクラスを使用して、Sessionプロパティ。Session information included in a session-state container is exposed to application code through the HttpSessionState class using the Session property. HttpSessionStateクラスはセッション状態コンテナーのラッパー クラスです。The HttpSessionState class is a wrapper class for a session-state container.

セッション状態コンテナーの ASP.NET の実装は、HttpSessionStateContainerクラス。The ASP.NET implementation of a session-state container is the HttpSessionStateContainer class. 要求の先頭の中に、AcquireRequestStateイベント、SessionStateModule作成および設定します、HttpSessionStateContainerオブジェクトし、現在に割り当てられますHttpContextします。At the beginning of a request, during the AcquireRequestState event, the SessionStateModule creates and populates an HttpSessionStateContainer object and assigns it to the current HttpContext. 要求の最後に中に、ReleaseRequestStateイベント、SessionStateModuleを取得、HttpSessionStateContainer現在からのオブジェクトHttpContextセッション ストアへのセッション値の書き込みや破棄など、セッションが必要な作業を実行し、セッションです。At the end of a request, during the ReleaseRequestState event, the SessionStateModule retrieves the HttpSessionStateContainer object from the current HttpContext and performs any required session work, such as writing the session values to the session store, or abandoning the session. 要求が突然終了した場合、このようなリダイレクトを介する、SessionStateModule呼び出すことによって同じクリーンアップを実行、EndRequestメソッド。If the request is abruptly terminated, such as through a redirect, the SessionStateModule performs the same cleanup by calling the EndRequest method.

カスタム セッション状態コンテナーを作成するには、実装するクラスを作成、IHttpSessionStateインターフェイス。To create a custom session-state container, create a class that implements the IHttpSessionState interface. 置き換える必要がありますが、独自のカスタム セッション状態コンテナーを作成する場合、SessionStateModule独自のカスタム モジュールを使用しています。If you are creating your own custom session-state container, you must also replace the SessionStateModule with your own custom module. カスタム モジュールは、カスタムのセッション状態コンテナーのインスタンスが作成され、現在に追加HttpContextを使用して、AddHttpSessionStateToContextメソッド。Your custom module will create an instance of your custom session-state container and add it to the current HttpContext using the AddHttpSessionStateToContext method. カスタム セッション状態モジュールの例が記載されて、SessionStateUtilityクラスの概要。An example of a custom session-state module is included in the SessionStateUtility class overview.

プロパティ

CodePage CodePage CodePage CodePage

現在のセッションのコード ページ識別子を取得または設定します。Gets or sets the code-page identifier for the current session.

CookieMode CookieMode CookieMode CookieMode

アプリケーションが Cookie なしのセッション用に構成されているかどうかを示す値を取得します。Gets a value that indicates whether the application is configured for cookieless sessions.

Count Count Count Count

セッション状態項目のコレクション内の項目数を取得します。Gets the number of items in the session-state item collection.

IsCookieless IsCookieless IsCookieless IsCookieless

セッション ID を URL に埋め込むか、HTTP クッキーに格納するかを示す値を取得します。Gets a value indicating whether the session ID is embedded in the URL or stored in an HTTP cookie.

IsNewSession IsNewSession IsNewSession IsNewSession

現在の要求でセッションが作成されたかどうかを示す値を取得します。Gets a value indicating whether the session was created with the current request.

IsReadOnly IsReadOnly IsReadOnly IsReadOnly

セッションが読み取り専用かどうかを示す値を取得します。Gets a value indicating whether the session is read-only.

IsSynchronized IsSynchronized IsSynchronized IsSynchronized

セッション状態値のコレクションへのアクセスが同期されている (スレッド セーフである) かどうかを示す値を取得します。Gets a value indicating whether access to the collection of session-state values is synchronized (thread safe).

Item[Int32] Item[Int32] Item[Int32] Item[Int32]

数値インデックスによってセッション状態項目値を取得または設定します。Gets or sets a session-state item value by numerical index.

Item[String] Item[String] Item[String] Item[String]

名前によってセッション状態項目値を取得または設定します。Gets or sets a session-state item value by name.

Keys Keys Keys Keys

セッション状態項目のコレクションに格納されているすべての値のキーのコレクションを取得します。Gets a collection of the keys for all values stored in the session-state item collection.

LCID LCID LCID LCID

現在のセッションのロケール識別子 (LCID) を取得または設定します。Gets or sets the locale identifier (LCID) of the current session.

Mode Mode Mode Mode

現在のセッション状態モードを取得します。Gets the current session-state mode.

SessionID SessionID SessionID SessionID

セッションの一意のセッション識別子を取得します。Gets the unique session identifier for the session.

StaticObjects StaticObjects StaticObjects StaticObjects

ASP.NET アプリケーションの Global.asax ファイルの <object Runat="Server" Scope="Session"/> タグで宣言されているオブジェクトのコレクションを取得します。Gets a collection of objects declared by <object Runat="Server" Scope="Session"/> tags within the ASP.NET application file Global.asax.

SyncRoot SyncRoot SyncRoot SyncRoot

セッション状態値のコレクションへのアクセスを同期するために使用できるオブジェクトを取得します。Gets an object that can be used to synchronize access to the collection of session-state values.

Timeout Timeout Timeout Timeout

セッション状態プロバイダーがセッションを終了するまでの、要求間で許容されるタイムアウト時間 (分単位) を取得または設定します。Gets or sets the time-out period (in minutes) allowed between requests before the session-state provider terminates the session.

メソッド

Abandon() Abandon() Abandon() Abandon()

現在のセッションを終了します。Ends the current session.

Add(String, Object) Add(String, Object) Add(String, Object) Add(String, Object)

新しい項目をセッション状態のコレクションに追加します。Adds a new item to the session-state collection.

Clear() Clear() Clear() Clear()

セッション状態項目のコレクションからすべての値をクリアします。Clears all values from the session-state item collection.

CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32)

セッション状態項目値のコレクションを 1 次元配列にコピーします。コピー操作は、指定した配列内のインデックスから始まります。Copies the collection of session-state item values to a one-dimensional array, starting at the specified index in the array.

GetEnumerator() GetEnumerator() GetEnumerator() GetEnumerator()

現在のセッションのすべてのセッション状態項目値を読み取るために使用する列挙子を返します。Returns an enumerator that can be used to read all the session-state item values in the current session.

Remove(String) Remove(String) Remove(String) Remove(String)

セッション状態項目のコレクションから項目を削除します。Deletes an item from the session-state item collection.

RemoveAll() RemoveAll() RemoveAll() RemoveAll()

セッション状態項目のコレクションからすべての値をクリアします。Clears all values from the session-state item collection.

RemoveAt(Int32) RemoveAt(Int32) RemoveAt(Int32) RemoveAt(Int32)

セッション状態項目のコレクションの指定したインデックス位置にある項目を削除します。Deletes an item at a specified index from the session-state item collection.

適用対象

こちらもご覧ください