首先,使用框架作的最好,能夠在框架頁直接作一次就行了jquery
再登錄成功後保存session的代碼後添加如下代碼:web
注意:須要引入命名空間using System.Collections;ajax
SetApplication("Online", clientContext.UserInfoID);json
而後寫SetApplication方法session
1 public static void SetApplication(string key, string value) 2 { 3 Hashtable hOnline = (Hashtable)HttpContext.Current.Application[key]; 4 if (hOnline != null) 5 { 6 IDictionaryEnumerator idE = hOnline.GetEnumerator(); 7 string strKey = ""; 8 while (idE.MoveNext()) 9 { 10 if (idE.Value != null && idE.Value.ToString().Equals(value)) 11 { 12 strKey = idE.Key.ToString(); 13 hOnline[strKey] = "XXXXXX"; 14 break; 15 } 16 } 17 } 18 else 19 { 20 hOnline = new Hashtable(); 21 } 22 hOnline[HttpContext.Current.Session.SessionID] = value; 23 HttpContext.Current.Application.Lock(); 24 HttpContext.Current.Application[key] = hOnline; 25 HttpContext.Current.Application.UnLock(); 26 }
登陸部分作好後開始在Global.asax頁面中添加代碼app
1 protected void Session_End(object sender, EventArgs e) 2 { 3 Hashtable hOnline = (Hashtable)Application["Online"]; 4 if (hOnline[Session.SessionID] != null) 5 { 6 hOnline.Remove(Session.SessionID); 7 Application.Lock(); 8 Application["Online"] = hOnline; 9 Application.UnLock(); 10 Session["ClientContext"] = null; 11 } 12 }
而後開始在框架頁寫代碼框架
我用到的是使用JQuery調用webservise方法進行驗證是否在其餘地方登陸url
jquery代碼以下:spa
首先使用定時器調用這個方法調試
1 $(document).ready(function () { 2 setInterval(CheckLogin, 5000); 3 }); 4 function CheckLogin() { 5 var parameters = { a: currentLoginUserID }; 6 $.ajax({ 7 type: "POST", 8 url: "/WebService/CSICommonWebService.asmx/CheckLogin", 9 contentType: "application/json; charset=utf-8", 10 dataType: "json", 11 data: JSON.stringify(parameters), 12 success: function (data) { 13 if (data.d != null && data.d != "" && data.d != undefined) { 14 alert('你的賬號已在別處登錄,你被強迫下線!'); window.open('/Index.aspx', '_parent'); 15 } 16 return true; 17 }, 18 error: function (ex) { 19 return false; 20 } 21 }); 22 23 }
而後在webservise(CSICommonWebService.asmx)頁面中添加下面代碼
1 [WebMethod(EnableSession = true)] 2 public string CheckLogin(string a) 3 { 4 Hashtable hOnline = (Hashtable)Application["Online"]; 5 if (hOnline != null) 6 { 7 IDictionaryEnumerator idE = hOnline.GetEnumerator(); 8 while (idE.MoveNext()) 9 { 10 if (idE.Key != null && idE.Key.ToString().Equals(Session.SessionID)) 11 { 12 if (idE.Value != null && "XXXXXX".Equals(idE.Value.ToString())) 13 { 14 hOnline.Remove(Session.SessionID); 15 Application.Lock(); 16 Application["Online"] = hOnline; 17 Application.UnLock(); 18 SessionLocator.Delete("ClientContext"); 19 return "你的賬號已在別處登錄,你被強迫下線!"; 20 } 21 } 22 } 23 return ""; 24 } 25 else 26 { 27 return ""; 28 } 29 }
作完這些之後,你能夠調試了,已經成功了,登錄以後在另外一個地方登陸,5秒鐘以內就會提醒下線,大功告成!!