漫天 flurrying

漫天 Asp.net C# flurrying 程序员 SEO asp java jsp javascript
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

一个账号不允许多个用户同时在线,重复登陆

Posted on 2010-10-13 14:52  漫天  阅读(2425)  评论(10编辑  收藏  举报
方法一:
代码 
 1 string sKey = username.Text.ToString().Trim(); // 得到Cache中的给定Key的值
 2 string sUser = Convert.ToString(Cache[sKey]); // 检查是否存在
 3 if (sUser == null || sUser == String.Empty)
 4 {
 5         TimeSpan SessTimeOut = new TimeSpan(00, System.Web.HttpContext.Current.Session.Timeout, 00);//取得Session的过期时间
 6         HttpContext.Current.Cache.Insert(sKey, sKey, null, DateTime.MaxValue, SessTimeOut, System.Web.Caching.CacheItemPriority.NotRemovable, null);//将值放入cache己方便单点登录
 7          //成功登录
 8 }
 9 else if (Cache[sKey].ToString() == sKey)//如果这个账号已经登录
10 {
11          ClientScript.RegisterStartupScript(GetType(), "提示""<script>alert('对不起,当前用户已经登录');</script>");
12          return;
13 }
14 else
15 {
16          Session.Abandon();//这段主要是为了避免不必要的错误导致不能登录
17 }

 

//关闭浏览器或窗口时清空Cache的方法.在主页面aspx文件中加入一个onunload事件.通过ajax清空hOnline中的Session.SessionID
window.onunload=function(){
 $.ajax({
         type: "POST",

   data:"sKey="+sKey;
         url: "online.aspx"
 });
}

online.aspx.cs代码

protected void Page_Load(object sender, EventArgs e)
        {

    HttpContext.Current.Cache.Remove(sKey);

        }

 //在Global.asax文件中的Session_End方法里加入

//Session过期后.清空hOnline中的Session.SessionID

    Hashtable hOnline = (Hashtable)Application["Online"];
            if (hOnline[Session.SessionID] != null)
            {
                hOnline.Remove(Session.SessionID);
                Application.Lock();
                Application["Online"] = hOnline;
                Application.UnLock();
            }

方法二:

 

代码
 1 //sKey为登录用户名
 2 if(ApplicationOnline(username.Text.tirm())){
 3     Hashtable hOnline = new Hashtable();
 4     hOnline[Session.SessionID] = sKey;
 5     Application.Lock();
 6     Application["Online"= hOnline;
 7     Application.UnLock();
 8 }
 9 
10 public Boolean ApplicationOnline(string sKey)
11         {
12             Boolean flag = true;
13             Hashtable hOnline = (Hashtable)Application["Online"];
14             if (hOnline != null)
15             {
16                 IDictionaryEnumerator idE = hOnline.GetEnumerator();
17                 while (idE.MoveNext())
18                 {
19                     //if (idE.Key != null && idE.Key.ToString().Equals(Session.SessionID))
20                     //{
21                     if (idE.Value != null && sKey.Equals(idE.Value.ToString()))
22                     {
23                         flag = false;
24                     }
25                     break;
26                     //}
27                 }
28             }
29             return flag;
30         }
31 
32 //关闭浏览器或窗口时清空Session.SessionID的方法.在主页面aspx文件中加入一个onunload事件.通过ajax清空Session.SessionID
33 window.onunload=function(){ 
34     $.ajax({
35             type: "POST",
36                  url: "online.aspx"
37     });
38 }

 

online.aspx.cs代码

protected void Page_Load(object sender, EventArgs e)
        {
            Hashtable hOnline = (Hashtable)Application["Online"];
            if (hOnline[Session.SessionID] != null)
            {
                hOnline.Remove(Session.SessionID);
                Application.Lock();
                Application["Online"] = hOnline;
                Application.UnLock();
            }
        }