由于对我们正在开发中的一些产品进行了渗透测试,当时看起来“容易”解决的问题变成了棘手的问题。
不是说它当然应该,我的意思是为什么只为当前生成一个全新的会话HTTPContext
如此困难?奇怪!不管怎样——我写了一个厚脸皮的小实用类来“做它”:
(为代码格式化/突出显示/ Visual Basic道歉,我一定做错了什么)
Imports System.Web
Imports System.Web.SessionState
Public Class SwitchSession
Public Shared Sub SetNewSession(ByVal context As HttpContext)
' This value will hold the ID managers action to creating a response cookie
Dim cookieAdded As Boolean
' We use the current session state as a template
Dim state As HttpSessionState = context.Session
' We use the default ID manager to generate a new session id
Dim idManager As New SessionIDManager()
' We also start with a new, fresh blank state item collection
Dim items As New SessionStateItemCollection()
' Static objects are extracted from the current session context
Dim staticObjects As HttpStaticObjectsCollection = _
SessionStateUtility.GetSessionStaticObjects(context)
' We construct the replacement session for the current, some parameters are new, others are taken from previous session
Dim replacement As New HttpSessionStateContainer( _
idManager.CreateSessionID(context), _
items, _
staticObjects, _
state.Timeout, _
True, _
state.CookieMode, _
state.Mode, _
state.IsReadOnly)
' Finally we strip the current session state from the current context
SessionStateUtility.RemoveHttpSessionStateFromContext(context)
' Then we replace the assign the active session state using the replacement we just constructed
SessionStateUtility.AddHttpSessionStateToContext(context, replacement)
' Make sure we clean out the responses of any other inteferring cookies
idManager.RemoveSessionID(context)
' Save our new cookie session identifier to the response
idManager.SaveSessionID(context, replacement.SessionID, False, cookieAdded)
End Sub
End Class
它对请求的其余部分工作正常,并正确地将自己标识为新会话(例如HTTPContext.Current.Session.SessionID
返回新生成的会话标识符)。
令人惊讶的是,当下一个请求到达服务器时,HTTPContext.Session
(一个HTTPSessionState
对象)将自己标识为正确的SessionID
,但已IsNewSession
设置为True
,并且为空,丢失了前一个请求中设置的所有会话值。
因此,从初始请求中删除的前一个对象一定有什么特别之处HTTPSessionState
,这里有一个事件处理程序,那里有一个回调,处理跨请求持久化会话数据的东西,或者只是我缺少的东西?
有人有什么魔法可以分享吗?