當用戶向一個網站請求第一個頁面時,用戶會話啟動。當第一個頁面被請求時,web服務器將
asp.net_sessionID cookie添加進用戶的瀏覽器。
可以使用newsession屬性探測新會話的啟動。當新會話啟動時,newsession屬性返回true。
response.write(session.newsession).
每個用戶會被分配一個唯一的會話ID,可以像下面這樣獲取會話ID的值
response.write(session.sessionid)
會話ID確保在所有當前用戶會話中是唯一的。但是,隨着時間的推移會話ID不保證唯一;可能將來的新會話循環使用會話ID
看:
protected void Application_Start(Object sender, EventArgs e) { Hashtable ht = new Hashtable(); Application["SessionIDs"] = ht; } protected void Session_Start(Object sender, EventArgs e) { Hashtable ht = (Hashtable) Application["SessionIDs"]; ht.Add( Session.SessionID, Session.SessionID ); } protected void Session_End(Object sender, EventArgs e) { Hashtable ht = (Hashtable) Application["SessionIDs"]; ht.Remove( Session.SessionID ); } in order to know if a session is alive we use this method: public bool IsSessionAlive( string id ) { Hashtable ht = (Hashtable) Application["SessionIDs"]; return ht.ContainsKey( id ); }