【发布时间】:2010-11-25 00:01:03
【问题描述】:
由于对我们正在开发中的一些产品进行了渗透测试,当时看起来“容易”解决的问题现在变成了棘手的问题。
当然不应该,我的意思是为什么为当前的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 对象一定有什么特别之处,这里有一个事件处理程序,那里有一个回调,处理跨请求持久化会话数据的东西,或者只是我缺少的东西?
有人有什么魔法可以分享吗?
【问题讨论】:
-
我通过给
SwitchSession类赋予一些状态(replacement会话)并为活动的 ASP.NET 应用程序实例连接SessionStateModule事件来改进我的SwitchSession类。当Start事件触发时,它会检查ASP.NET 生成的会话是否具有相同的SessionID,并将前一个请求中的所有会话状态值复制到其中。显然,只有当所有请求都来自处理前一个请求的HTTPApplication实例时才有效。我正在使用反射器对SessionStateModule进行更深入的挖掘,但它并不漂亮。请为这个问题投票! -
我到了和你差不多的地方(通过搜索 RemoveHttpSessionStateFromContext 到达你的页面)。不幸的是,你也遇到了同样的问题 - 似乎无法生成新的会话。关键当然是 SessionStateModule.CompleteAcquiredState(),这很难实现 - Yudhi 的反射方法将是实现它的一种方法,但我不确定它是否值得麻烦。我必须说,尽管我非常喜欢 C#,但 .NET 的 API 却是一个巨大的失望——他们怎么能不公开这个!
-
仅供参考:CompleteAcquiredState() 调用 SessionStateUtility.AddDelayedHttpSessionStateToContext(),它为新会话发挥了所有作用。
标签: asp.net session-state sessionid httpsession