限制同一账号多处登录

原文:

http://wenku.baidu.com/link?url=x0keBGHull3vEYGXQxVyYJEW9iRMnkcHGMph3DquT9Uc0Y4cGh0CNmlIIa4trsmQEufXR0Cm9CSLfhptTF144OLf07V3CNav4hcoeowHhTW#

SignOnMode.cs

#region
// -----------------------------------------------------------------
// 版权所有:Copyright(C)
// 文件名称:SignOnMode.cs
// 系统名称:
// 模块名称:
// 作  者:Keasy
// 完成日期:2015/12/24
// 功能描述:
// -----------------------------------------------------------------
// 修 改 者:
// 修改日期:
// 修改描述:
// -----------------------------------------------------------------
#endregion

using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using DocumentFormat.OpenXml.Packaging;

namespace Chinasap.Columbus.Common.Function.Login
{
    /// <summary>
    /// 登录方式类
    /// </summary>
    /// <remarks>
    /// 同一账号是否能在多处登录帮助类
    /// </remarks>
    public static class SignOnMode
    {
        /// <summary>
        /// 在线信息Application标识
        /// </summary>
        private const string OnlineAppFlag = "Online";

        private const string InvalidSessionFlag = "INVALIDSESSION";

        /// <summary>
        /// 添加登录信息
        /// </summary>
        /// <param name="signOnToken">
        /// 登录令牌:用于标识用户信息的唯一标识,一般是用户的id
        /// </param>
        public static void RegisterSignOn(string signOnToken)
        {
            Hashtable hOnline = (Hashtable)HttpContext.Current.Application[OnlineAppFlag];
            if (hOnline != null)
            {
                IDictionaryEnumerator enumerator = hOnline.GetEnumerator();
                string sessionId = "";
                while (enumerator.MoveNext())
                {
                    if (enumerator.Value != null && enumerator.Value.ToString().Equals(signOnToken))
                    {
                        /*
                         * 将当前用户Id对应的会话(Session.SessionID唯一标识)
                         * 都设置为无效会话
                         */
                        sessionId = enumerator.Key.ToString();
                        hOnline[sessionId] = InvalidSessionFlag; //无效会话
                        break;
                    }
                }
            }
            else
            {
                hOnline = new Hashtable();
            }

            //将当前"登录"用户Id对应的会话设置为有效会话
            hOnline[HttpContext.Current.Session.SessionID] = signOnToken;
            HttpContext.Current.Application.Lock();
            HttpContext.Current.Application["Online"] = hOnline;
            HttpContext.Current.Application.UnLock();
        }

        /// <summary>
        /// 执行登录规则
        /// </summary>
        /// <returns>
        /// 是否退出登录:
        /// true:表示当前登录需要当前强制退出
        /// 否则:当前当前登录不需要退出
        /// </returns>
        public static bool ExecuteSignOnRule()
        {
            bool forcedLogout = false; //是否需要强制退出当前登录

            // 配置:同一账号是否能在多处登录
            string canMultipleLogin = ConfigurationManager.AppSettings["CanMultipleLogin"];
            if (canMultipleLogin != null && canMultipleLogin.ToLower() == "true")
            {
                return false; //同一账号可以能在多处登录
            }

            Hashtable hOnline = (Hashtable)HttpContext.Current.Application["Online"];
            if (hOnline != null && HttpContext.Current.Session != null)
            {
                IDictionaryEnumerator enumerator = hOnline.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    var sessionId = enumerator.Key;
                    var sessionVal = enumerator.Value;

                    if (sessionId != null && sessionId.ToString().Equals(HttpContext.Current.Session.SessionID))
                    {
                        if (sessionVal != null && InvalidSessionFlag.Equals(sessionVal.ToString()))
                        {
                            /*删除无效会话*/
                            hOnline.Remove(HttpContext.Current.Session.SessionID);
                            HttpContext.Current.Application.Lock();
                            HttpContext.Current.Application["Online"] = hOnline;
                            HttpContext.Current.Application.UnLock();

                            //帐号已在别处登陆,被强迫下线!
                            HttpContext.Current.Session[SessionKeys.LoginUser] = null;
                            forcedLogout = true;
                        }
                        break;
                    }
                }
            }

            return forcedLogout;
        }

        /// <summary>
        /// 清除无效登录信息
        /// </summary>
        /// <remarks>
        /// 一般用于Global.asax.cs的函数:
        ///     protected void Session_End(object sender, EventArgs e)
        ///  中调用,使得在Session过期或者退出系统时释放资源
        /// </remarks>
        public static void ClearInvalidSignOn()
        {
            if (HttpContext.Current != null)
            {
                Hashtable hOnline = (Hashtable)HttpContext.Current.Application["OnlineAppFlag"];
                if (hOnline != null)
                {
                    if (HttpContext.Current.Session != null)
                    {
                        if (hOnline[HttpContext.Current.Session.SessionID] != null)
                        {
                            hOnline.Remove(HttpContext.Current.Session.SessionID);
                            HttpContext.Current.Application.Lock();
                        }
                    }
                }
            }
        }
    }
}

使用:

第一步:在登录成功后,记录登录信息

  SignOnMode.RegisterSignOn(user.Id);

 public string Login(LoginViewModel model,bool rememberAccount)
 {
    //.......
     if(登录成功)
    {
          SignOnMode.RegisterSignOn(user.Id);
     }
    //.......
}

第二步:

在需要登录的地方执行

bool forcedLogout = SignOnMode.ExecuteSignOnRule();
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var _url = "~/Home/Index";

            if (SessionHelper.GetSession(SessionKeys.LoginUser) == null)
            {
                filterContext.Result = new RedirectResult(_url);
            }
            else
            {
                #region 同一账号不能多处登录
                /*--------------------------------------------------------------------------
                 同一账号不能多处登录
                 *--------------------------------------------------------------------------*/

                bool forcedLogout = SignOnMode.ExecuteSignOnRule();
                if (forcedLogout)
                {
                    filterContext.Result = new RedirectResult(_url);
                }

                #endregion
            }
        }

第三步:在Session过期或者(异常)退出系统时,释放资源释放资源,做法是:

在Global.asax.cs的函数:

  protected void Session_End(object sender, EventArgs e)

中调用,使得在Session过期或者退出系统时释放资源

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {            ....
        }

        protected void Session_End(object sender, EventArgs e)
        {
            /*--------------------------------------------------------------------------
              同一账号不能多处登录
            *--------------------------------------------------------------------------*/
            /*在Session过期或者退出系统时释放资源*/
            SignOnMode.ClearInvalidSignOn();
        }
    }

第四步:

  在配置文件添加中添加“同一账号是否能多处登录”的开关。

  <appSettings>
    <!--配置:同一账号是否能多处登录,true表示可以多点登陆-->
    <add key="CanMultipleLogin" value="false" />
  </appSettings>

完毕。

时间: 2024-08-07 17:00:18

限制同一账号多处登录的相关文章

struts2的记住账号密码的登录设计

一个简单的基于struts2的登录功能,实现的额外功能有记住账号密码,登录错误提示.这里写上我在设计时的思路流程,希望大家能给点建设性的意见,帮助我改善设计. 登录功能的制作,首先将jsp界面搭建出来,界面搭建出来之后,我首先想的是如何用cookie来实现记住账号密码,这里我选择的是在class中实现这个cookie. jsp页面中的checkbox标签作为一个标记参数来判断用户是否选择了记住账号和密码,在登录提交之后,伴随着登录的账号.密码.激活码一起传递到action中,checkbox选中

jquery记住密码,记住账号,自动登录

1.引入jquery库 2.引入jquery.cookie.js库 3.引入操作js jsp如下: $(document).ready(function() { //输入框获得焦点-失去焦点 $(".oaText").focus(function(){ oaFocus(".oaText","请输入用户名"); }); $(".oaText").blur(function(){ oaBlur(".oaText"

Centos6.5部署openvpn账号密码方式登录

server端(路由模式): 一.网络设置 1.开启服务器端路由转发功能 # vi /etc/sysctl.conf net.ipv4.ip_forward = 1 # sysctl -p 2.设置nat转发: 注:保证VPN地址池可路由出外网 # iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERAD 3.时间同步: # ntpdate asia.pool.ntp.org 二.安装依赖库 # yum install

Win10 域账号使用指纹登录

首先使用本地管理员账号按如下步骤操作: 在"运行"里面输入gpedit.msc,打开组策略: 选择"计算机配置-管理模板-Windows组件-生物特征",在右侧窗口设置中双击"允许域用户使用生物特征登录",设备为"已启动"并保存. 然后,使用本地管理员账号,新建一个.reg尾缀(新建txt文件,然后重命名reg文件)的文件,文件内容如下: Windows Registry Editor Version 5.00 [HKEY_L

web应用中实现同一个账号,后面登录的会把前面登录的挤下线

在web应用中假如没有做会话控制,会出现这样的情况,A登录了账号,B也登录了账号,都是同样的账号,A修改了信息,B会看到修改的信息,这样的用户体验不好,B会觉得我没有修改啊,为什么信息会改变.而做会话控制后,A先登录,B再登录,那么B会把A的登录挤下线. 实现思路:采用时间戳比较 1首先用户登录时,后台是不需要做拦截的,前台把用户名和密码传到后台,后台生成JWT格式的token给前台,并以token为key,用户信息为value存入redis中 2其他url路径,过滤器会拦截请求,先判断前台是否

eclipse svn插件 删除原账号信息重新登录

1.通过删除SVN客户端的账号配置文件 1)查看你的Eclipse中使用的是什么SVN Interface(中文:svn接口)windows > preference > Team > SVN 在右边的设置面板中可以看到SVN Interface或中文的svn接口一栏,Client的选项框中显示的就是你当前用的svn接口 2)如果是用的JavaHL, 找到以下目录 C:Documents and Settings用户名Application DataSubversion应看到有一个aut

获取联系人+把注册的账号密码放在登录页面

item自己添加布局: <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" andr

ubuntu18设置root账号的开机登录

date: 2019-08-20  17:36:49 author: headsen chen notice :个人原创 1,用普通用户登录. su - root 打开终端 vi /etc/pam.d/gdm-autologin 2,vi /etc/pam.d/gdm-password 3,vim /root/.profile 4,reboot  重启系统就可以了 原文地址:https://www.cnblogs.com/kaishirenshi/p/11384502.html

burp suite爆破账号密码之登录用户密码

Burp 开启监听模式 浏览器配置好burp的监听端口代理之后 输入账号admin,密码随便输入 点击登录后,burp会拦截到请求信息 右键发送到intruder 然后点击进入intruder 等待爆出完成 关闭burp监听,进行输入验证 登录成功. 原文地址:https://www.cnblogs.com/mrlpl/p/12293661.html