• 限制帐号同时两处以上登录-ASP.NET


     ///登录页面
    Hashtable haol = (Hashtable)Application["olTable"];
                    if (haol == null)
                    {
                        haol = new Hashtable();
                        Application["olTable"] = haol;
                    }
                    else
                    {
    
                        HttpCookie cookie = new HttpCookie("ZInfo");//定义cookie对象以及名为Info的项  
                        if (Request.Cookies["Info"] != null)
                        {
                            Request.Cookies["Info"].Values["UserHotleID"] = SessionInclude.UserBranchId;
                            //cookie.Values.Add("UserHotleID", SessionInclude.UserBranchId);
                            Response.AppendCookie(cookie);//确定写入cookie中 
                        }
                        else
                        {
                            cookie.Values.Add("UserHotleID", SessionInclude.UserBranchId);
                            Response.AppendCookie(cookie);//确定写入cookie中 
                        }
    
                        //当前用户标识
                        Guid key = Guid.NewGuid();
                        Session["userKey"] = key;
                        IDictionaryEnumerator list = haol.GetEnumerator();
                        ArrayList ls = new ArrayList();
                        while (list.MoveNext())
                        {
                            if (list.Value.ToString() == SessionInclude.SessionId.Trim())
                            {
                                ls.Add(list.Key);
                                //haol.Remove(list.Key);
                            }
                        }
                        int i = ls.Count - 1;
                        while (i > -1)
                        {
                            Application.Lock();
                            haol.Remove(ls[i]);
                            Application.UnLock();
                            i = i - 1;
                        }
                        Application.Lock();
                        haol.Add(key, SessionInclude.SessionId.Trim());
                        Application["olTable"] = haol;
                        Application.UnLock();
                    }
       ///所有页面继承类
    public class BasePage : System.Web.UI.Page
    {
     protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }
    
        override protected void OnInit(EventArgs e)
        {
            BasePageInitInfo();
        }
    public void BasePageInitInfo()
        {
            try
            {
                Hashtable haol = (Hashtable)Application["olTable"];
                if (haol == null)
                {
                    haol = new Hashtable();
                    Application["olTable"] = haol;
                }
                else
                {
                    if (Session["userKey"] != null)
                    {
                        //当前用户Key
                        Guid key = new Guid(Session["userKey"].ToString());
                        IDictionaryEnumerator val = haol.GetEnumerator();
                        while (val.MoveNext())
                        {
                            if (val.Value.ToString().Trim() == SessionInclude.SessionId.Trim())
                            {
                                Guid currGuid = new Guid(val.Key.ToString());
                                if (!currGuid.Equals(key))
                                {
                                    Application.Lock();
                                    haol.Remove(Session["userKey"].ToString());
                                    Session["userKey"] = null;
                                    Application.UnLock();
                                    Response.Write("<script>alert('您的帐号在别处登录,您被迫下线');window.parent.location='../Login.aspx';</script>");
                                    break;
                                }
                            }
                        }
                    }
                    else
                    {
                        //当前用户标识
                        Guid key = Guid.NewGuid();
                        Session["userKey"] = key;
                        IDictionaryEnumerator list = haol.GetEnumerator();
                        ArrayList ls = new ArrayList();
                        while (list.MoveNext())
                        {
                            if (list.Value.ToString() == SessionInclude.SessionId.Trim())
                            {
                                ls.Add(list.Key);
                                //haol.Remove(list.Key);
                            }
                        }
                        int i = ls.Count - 1;
                        while (i > -1)
                        {
                            Application.Lock();
                            haol.Remove(ls[i]);
                            Application.UnLock();
                            i = i - 1;
                        }
                        Application.Lock();
                        haol.Add(key, SessionInclude.SessionId.Trim());
                        Application["olTable"] = haol;
                        Application.UnLock();
                    }
                }
            }
            catch
            {
    
            }
    
        }
    }
  • 相关阅读:
    Linux curl命令详解
    drools 7 日期时间段校验
    java程序员常用查询和学习的网站
    springboot+mybatis+分页插件的使用
    Vue. 之 替换 左上角 title标签处的图标
    Github访问速度很慢的原因,以及解决方法
    vue插件总结——总有你能用上的插件
    在Java中如何高效的判断数组中是否包含某个元素
    解决vue刷新页面以后丢失store的数据
    vue路由跳转时组件data数据刷新
  • 原文地址:https://www.cnblogs.com/elves/p/4044947.html
Copyright © 2020-2023  润新知