パイプラインにある一部の製品に対する侵入テストの結果、当時は「簡単に」修正できると思われていた問題が、難しいものであることが判明しました。
もちろん、そうすべきではありませんが、現在の新しいセッションを生成する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したがって、最初のリクエストから削除される前のオブジェクト、ここのイベント ハンドラー、そこのコールバック、複数のリクエスト間でセッション データの永続化を処理する何か、または私が欠けている何かについて何か特別なものがあるに違いありません。
共有する魔法を持っている人はいますか?