zoukankan      html  css  js  c++  java
  • 防止用户重复登陆

    防止用户重复登陆

    一: 设置Global.asax文件 
    处理Application_Start方法,实例化一个哈西表,然后保存在Cache

     

     

       

    protected void Application_Start(Object sender, EventArgs e) 
        

           Hashtable h
    =new Hashtable(); 
           Context.Cache.Insert(
    "online",h); 
       }
     

     

     

     

     

        Session_End方法里调用LogoutCache()方法,方法源码如下

     

    /// <summary>

     

     

        /// 清除Cache里当前的用户,主要在Global.asaxSession_End方法和用户注销的方法里调用      ///</summary> 

        public void LogoutCache() 
        

           Hashtable h
    =(Hashtable)Context.Cache["online"]; 
           
    if(h!=null
           

               
    if(h[Session.SessionID]!=null
               h.Remove(Session.SessionID); 
               Context.Cache[
    "online"]=h; 
           }
     
        }
     

     

     

    二:

    登陆前调用PreventRepeatLogin()方法,这个方法可以防止用户重复登陆,如果上次用户登陆超时大于1分钟,也就是关闭了所有admin目录下的页面达到60秒以上,就认为上次登陆的用户超时,你就可以登陆了,如果不超过60秒,就会生成一个自定义异常。在Cache["online"]里保存了一个哈西表,哈西表的key是当前登陆用户的SessionID,Value是一个ArrayList,这个ArrayList有两个元素,第一个是用户登陆的名字第二个元素是用户登陆的时间,然后在每个admin目录下的页刷新页面的时候会更新当前登陆用户的登陆时间,而只admin目录下有一个页打开着,即使不手工向服务器发送请求,也会自动发送一个请求更新登陆时间,下面我在页面基类里写了一个函数来做到这一点,其实这会增加服务器的负担,但在一定情况下也是一个可行的办法.

     

     

     

     

           /// <summary> 
           
    /// 防止用户重复登陆,在用户将要身份验证前使用 
            
    /// </summary> 
           
    /// <param name="name">要验证的用户名字</param> 

           private void PreventRepeatLogin(string name) 
           

               Hashtable h
    =(Hashtable)Cache["online"]; 
               
    if(h!=null
               

                  IDictionaryEnumerator e1
    =h.GetEnumerator(); 
                  
    bool flag=false
                  
    while(e1.MoveNext()) 
                  
    {
                      
    if((string)((ArrayList)e1.Value)[0]==name) 
                      

                         flag
    =true
                         
    break
                      }
     
                  }
     
                  
    if(flag) 
                  

                      TimeSpan ts
    =System.DateTime.Now.Subtract(Convert.ToDateTime(((ArrayList)e1.Value)[1])); 
                      
    if(ts.TotalSeconds<60
                        Response.Write(
    "<SCRIPT language="JavaScript">alert('有相同用户登陆');</SCRIPT>"); 
                      
    else 
                         h.Remove(e1.Key); 
                  }
     
               }
     
               
    else 
               

                  h
    =new Hashtable(); 
               }
     
               ArrayList al
    =new ArrayList(); 
               al.Add(name); 
               al.Add(System.DateTime.Now); 
               h[Session.SessionID]
    =al; 
               
    if(Cache["online"]==null
               

                  Context.Cache.Insert(
    "online",h); 
               }
    else 
                  Cache[
    "Online"]=h;          
          }
     

    用户注销的时候调用上面提到LogoutCache()方法

  • 相关阅读:
    编程的智慧(王垠)(http://www.cocoachina.com/programmer/20151125/14410.html)
    NSString用法,object-C数组以及字符串拼接和分割
    xcode自动生成代码片段
    21 RadioGroup ListFragment
    21 PagerTabStrip-PagerTitleStrip-viewPager
    21 FragmentTabHost +Fragment代码案例
    21 导航书签一些总结
    Udemy上免费的angualr2视频教程分享
    撕衣服源码
    android viewpager切换到最后一页时,跳转至其他activity
  • 原文地址:https://www.cnblogs.com/sczw-maqing/p/3193124.html
Copyright © 2011-2022 走看看