ASP.net MVC实现单点登录

ASP.net MVC实现单点登录

单点登录效果:同一个账号在不同设备登录时,后登录的设备将前面登录的设备账号挤下线。
在这里插入图片描述

1.登录时在服务器保存登录数据

Name是用户的一个唯一标识

  private void GetOnline(string Name)
  {
      Hashtable SingleOnline = (Hashtable)System.Web.HttpContext.Current.Application["Online"];
      if (SingleOnline == null)
      {
          SingleOnline = new Hashtable();
      }
      if (SingleOnline.ContainsKey(Name))
      {
          SingleOnline[Name] = Session.SessionID;
      }
      else
      {
          SingleOnline.Add(Name, Session.SessionID);
      }
      Session["user"] = Name;
      System.Web.HttpContext.Current.Application.Lock();
      System.Web.HttpContext.Current.Application["Online"] = SingleOnline;
      System.Web.HttpContext.Current.Application.UnLock();
  }
2.全局过滤请求判断当前账号是否在其它地方登录

APP_Start 下新建 FilterConfig.cs
排除部分请求(如登录请求不需要进行过滤拦截)

 public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new LoginFilterAttribute() { IsCheck = true });
}

在不需要过滤的Action前加上以下代码

[LoginFilter(IsCheck = false)]

过滤验证

public bool IsCheck { get; set; }
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    if (!IsCheck)
    {
        return;
    }
    string ss = filterContext.HttpContext.Session["user"] as string;
    if (ss == null || ss == "")
    {
        filterContext.Result = new ContentResult() { Content = "<script>window.location.href='../../../Main/load'</script>" };
        return;
    }
    Hashtable singleOnline = (Hashtable)filterContext.HttpContext.Application["Online"];
    // 判断当前SessionID是否存在
    if (singleOnline != null && singleOnline.ContainsKey(ss))
    {
        if (!singleOnline[ss].Equals(filterContext.HttpContext.Session.SessionID))
        {
            filterContext.Result = new ContentResult() { Content = "<script>alert('您的帐号已在别处登录 ,将被迫下线(请保管好自己的账号密码)!');window.location.href='xxx';</script>" };
        }
    }
    base.OnActionExecuting(filterContext);
    Hashtable hOnline = (Hashtable)HttpContext.Current.Application["Online"];
    if (hOnline != null)
    {
        IDictionaryEnumerator idE = hOnline.GetEnumerator();
        while (idE.MoveNext())
        {
            if (idE.Key != null && idE.Key.ToString().Equals(HttpContext.Current.Session.SessionID))
            {
                //already login  
                if (idE.Value != null && "XXXXXX".Equals(idE.Value.ToString()))
                {
                    hOnline.Remove(HttpContext.Current.Session.SessionID);
                    HttpContext.Current.Application.Lock();
                    HttpContext.Current.Application["Online"] = hOnline;
                    HttpContext.Current.Application.UnLock();
                    filterContext.Result = new ContentResult()
                    {
                        Content = "<script>alert('您的帐号已在别处登录 ,将被迫下线(请保管好自己的账号密码)!');window.location.href='xxx'</script>"
                    };
                }
                break;
            }
        }
    }
    base.OnActionExecuting(filterContext);
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值