Export (0) Print
Expand All

IHttpSessionState Interface

Defines the contract to implement a custom session-state container.

Namespace:  System.Web.SessionState
Assembly:  System.Web (in System.Web.dll)

'Declaration
Public Interface IHttpSessionState

The IHttpSessionState type exposes the following members.

  NameDescription
Public propertyCodePageGets or sets the code-page identifier for the current session.
Public propertyCookieModeGets a value that indicates whether the application is configured for cookieless sessions.
Public propertyCountGets the number of items in the session-state item collection.
Public propertyIsCookielessGets a value indicating whether the session ID is embedded in the URL or stored in an HTTP cookie.
Public propertyIsNewSessionGets a value indicating whether the session was created with the current request.
Public propertyIsReadOnlyGets a value indicating whether the session is read-only.
Public propertyIsSynchronizedGets a value indicating whether access to the collection of session-state values is synchronized (thread safe).
Public propertyItem(Int32)Gets or sets a session-state item value by numerical index.
Public propertyItem(String)Gets or sets a session-state item value by name.
Public propertyKeysGets a collection of the keys for all values stored in the session-state item collection.
Public propertyLCIDGets or sets the locale identifier (LCID) of the current session.
Public propertyModeGets the current session-state mode.
Public propertySessionIDGets the unique session identifier for the session.
Public propertyStaticObjectsGets a collection of objects declared by <object Runat="Server" Scope="Session"/> tags within the ASP.NET application file Global.asax.
Public propertySyncRootGets an object that can be used to synchronize access to the collection of session-state values.
Public propertyTimeoutGets and sets the time-out period (in minutes) allowed between requests before the session-state provider terminates the session.
Top

  NameDescription
Public methodAbandonEnds the current session.
Public methodAddAdds a new item to the session-state collection.
Public methodClearClears all values from the session-state item collection.
Public methodCopyToCopies the collection of session-state item values to a one-dimensional array, starting at the specified index in the array.
Public methodGetEnumeratorReturns an enumerator that can be used to read all the session-state item values in the current session.
Public methodRemoveDeletes an item from the session-state item collection.
Public methodRemoveAllClears all values from the session-state item collection.
Public methodRemoveAtDeletes an item at a specified index from the session-state item collection.
Top

A session-state container provides access to the session-state values and related information for the current session. Session information included in a session-state container is exposed to application code through the HttpSessionState class using the Session property. The HttpSessionState class is a wrapper class for a session-state container.

The ASP.NET implementation of a session-state container is the HttpSessionStateContainer class. 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. 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. If the request is abruptly terminated, such as through a redirect, the SessionStateModule performs the same cleanup by calling the EndRequest method.

To create a custom session-state container, create a class that implements the IHttpSessionState interface. If you are creating your own custom session-state container, you must also replace the SessionStateModule with your own custom module. Your custom module will create an instance of your custom session-state container and add it to the current HttpContext using the AddHttpSessionStateToContext method. An example of a custom session-state module is included in the SessionStateUtility class overview.

The following code example implements the IHttpSessionState interface to create a new session-state container class named MySessionState.

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

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft