asp.net によって生成された既存のセッション ID を変更しなければならないという問題があります。
自分のセッション ID を割り当てる必要があります。
セッションIDマネージャー(createsession
およびvalidate
)のメソッドをオーバーライドしてみましたが、それでも既存のセッションIDがセッションに保持されています。その後、リフレクションを使用していくつかの内部フィールドを処理するコードを見てきました。
しかし、それでも同じ結果の古いセッションIDはありますか?
私は次のコードを取りました
HttpSessionState ss = HttpContext.Current.Session;
HttpContext.Current.Session["test"] = "test";
HttpContext.Current.Response.Write(ss.SessionID);
SessionIDManager manager = new SessionIDManager();
manager.RemoveSessionID(System.Web.HttpContext.Current);
ss.Clear();
Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", ""));
MysessionId myId = new MysessionId();
bool redirected = false;
bool IsAdded = false;
string newid = myId.CreateSessionID(System.Web.HttpContext.Current);
myId.SaveSessionID(System.Web.HttpContext.Current, newid, out redirected, out IsAdded);
HttpApplication ctx = (HttpApplication)HttpContext.Current.ApplicationInstance;
HttpModuleCollection mods = ctx.Modules;
System.Web.SessionState.SessionStateModule ssm = (SessionStateModule)mods.Get("Session");
System.Reflection.FieldInfo[] fields = ssm.GetType().GetFields( BindingFlags.NonPublic|BindingFlags.Instance);
SessionStateStoreProviderBase store = null;
System.Reflection.FieldInfo rqIdField = null, rqLockIdField = null, rqStateNotFoundField = null;
foreach (System.Reflection.FieldInfo field in fields)
{
if (field.Name.Equals("_store")) store = (SessionStateStoreProviderBase)field.GetValue(ssm);
if (field.Name.Equals("_rqId")) rqIdField = field;
if (field.Name.Equals("_rqLockId")) rqLockIdField = field;
if (field.Name.Equals("_rqSessionStateNotFound")) rqStateNotFoundField = field;
}
object lockId = rqLockIdField.GetValue(ssm);
if ((lockId != null) && (ss.SessionID != null)) store.ReleaseItemExclusive(Context, ss.SessionID, lockId);
rqStateNotFoundField.SetValue(ssm, true); rqIdField.SetValue(ssm, newid);
HttpSessionState s2 = HttpContext.Current.Session;
lblMessage.Text = "changed session id is :" + s2.SessionID;
ここで MysessionId は、create メソッドと validate{return true} メソッドをオーバーライドしたクラスです