First question in a long time.

I was wondering if anyone know how to search all current sessions within the website?

I want to be able to loop through all the current sessions (with a specific session name if possible) on my current website at one time.

I am trying to create a script that will search all current sessions to login/logout via the database depending on the value of the session.

Basically, most of you have realized that relying on "Session_End" in the global.asax file is not very easy as most Session_Ends never fire. Session_Start almost always fires, but Session_End isn't as lucky.

I will be creating a script that runs through all current sessions to get certain information relative to a specific need.

So if you know how or can guide me in the right direction of searching all current sessions with a given name (let's say Session("userid")) within the current website, then please help.

Thank you.

Dani AI

Generated

asked how to enumerate sessions (for example to find Session("userid")) and pointed to a useful MSDN debugging script. The reality is that ASP.NET does not expose a supported API to enumerate all in-process sessions — session state is private to the session store. That makes two practical, reliable approaches for production:

  • Track session metadata explicitly in a central store (database or distributed cache) and update it on login and on each request.
  • If using out-of-process session state, persist the user id in a separate, queryable table rather than trying to parse the session blob.

A minimal, production-friendly pattern is to record/refresh an ActiveSessions row on every request (via an IHttpModule or in Global.asax). Example (VB) for an AcquireRequestState handler that updates a DB/registry entry:

Public Class SessionTrackerModule
    Implements IHttpModule

    Public Sub Init(context As HttpApplication) Implements IHttpModule.Init
        AddHandler context.AcquireRequestState, AddressOf OnAcquireRequestState
    End Sub

    Private Sub OnAcquireRequestState(sender As Object, e As EventArgs)
        Dim app = CType(sender, HttpApplication)
        Dim s = app.Context.Session
        If s IsNot Nothing Then
            Dim sid = s.SessionID
            Dim userId = If(s("userid"), DBNull.Value)
            ' Upsert into ActiveSessions table: SessionId, UserId, LastRequestUtc
            ' Use parameterized SQL or an ORM here
        End If
    End Sub

    Public Sub Dispose() Implements IHttpModule.Dispose
    End Sub
End Class

A lighter in-process trick is to create a Cache entry per session with the same timeout and a removal callback (treat the callback as a session-expired hook). This is still per-process and suffers the same app-recycle limitations as Session_End, so it’s best for diagnostics rather than cross-server/robust tracking.

Recommendations and cautions: do not rely on Session_End (fires only for InProc and not on abrupt process shutdown). For true multi-server or recycle-resistant tracking use a database or distributed cache (Redis, SQL). Store the user id separately for easy querying, update a LastActivity timestamp per request, and run a cleanup job to mark expired sessions. Secure and index the ActiveSessions table to avoid performance or data-leak issues.

Recommended Answers

All 3 Replies

fabulous! thanks a ton serk.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.