また、より効率的なキャッシングを使用するカスタム セッション プロバイダーもあったので、それを利用して自分でセッションを作成することができました。他の誰かが興味を持っている場合は、これが私のコードですが、借りたい場合は自分で微調整する必要があることに注意してください。
public void EnsureSessionExists(HttpContext context) {
if (context.Session != null) {
// Hey, we've already got a session. That was easy...
return;
}
bool isNew = false;
string sesId = _sessionIdManager.GetSessionID(context);
if (String.IsNullOrEmpty(sesId)) {
// if ID is null or empty, it means we're creating a new session?
sesId = _sessionIdManager.CreateSessionID(context);
}
SessionStateStoreData data = GetSessionDataStore(context, sesId);
if (data == null) {
isNew = true;
data = CreateNewStoreData(context, _sessionTimeout);
// Create doesn't put it in the cache. This does.
SetSessionDataStore(context, sesId, data);
}
HttpSessionStateContainer container = new HttpSessionStateContainer(sesId, data.Items, data.StaticObjects, data.Timeout, isNew, HttpCookieMode.UseCookies, SessionStateMode.Custom, false);
SessionStateUtility.AddHttpSessionStateToContext(context, container);
// Force the cookie to get set here. SessionStateModule only sets it if the Handler has IRequiresSessionState
HttpCookie cookie = new HttpCookie("ASP.NET_SessionId", _sessionIdManager.Encode(sesId));
cookie.Expires = DateTime.MinValue; // DateTime.MinValue makes it a session cookie.
context.Response.Cookies.Add(cookie);
}
パズルの大きなピースは、SessionStateStoreData オブジェクトの取得に関する部分です。デフォルトの実装を使用している場合は、.NET リフレクターを調べてアクセス方法を見つけてください。
また、これの欠点の 1 つは、セッションが実際に変更された後にのみ SessionStateModule が Cookie を作成することです。ただし、このコードを実行すると、実際には非常にまれなケースでのみセッションを使用しているにもかかわらず、Cookie を常に作成する必要があります。