实现类似QQ单一账户登录,在另一个地方登录后在原登录窗口提示下线

首先,使用框架做的最好,可以在框架页直接做一次就好了

再登陆成功后保存session的代码后添加以下代码:

注意:需要引入命名空间using System.Collections;

SetApplication("Online", clientContext.UserInfoID);

然后写SetApplication方法

 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页面中添加代码

 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方法进行验证是否在其他地方登录

jquery代码如下:

首先使用定时器调用这个方法

 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秒钟之内就会提醒下线,大功告成!!

posted @ 2013-09-27 10:51  shixudong  阅读(2875)  评论(3编辑  收藏  举报