MVC中单用户登录

14 篇文章 0 订阅

把下面这段代码放在登录用户验证以后:

复制代码
//用户登录验证通过后判断用户是否重复登录
        public void SingleUserCheck(string userid)
        {
            HttpContext httpContext = System.Web.HttpContext.Current;
            Hashtable userOnline = (Hashtable)httpContext.Application["Online"];
            if (userOnline != null)
            {
                int i = 0;
                while (i < userOnline.Count)
                {
                    IDictionaryEnumerator idE = userOnline.GetEnumerator();
                    string strKey = string.Empty;
                    while (idE.MoveNext())
                    {
                        if (idE.Value != null && idE.Value.ToString().Equals(userid))  //如果当前用户已经登录,
                        {
                            strKey = idE.Key.ToString();
                            userOnline[strKey] = "XXXXXX";   //将当前用 户已经在全局变量中的值设置为XX 
                            break;
                        }
                    }
                    i++;
                }
            }
            else
            {
                userOnline = new Hashtable();
            }
            userOnline[httpContext.Session.SessionID] = userid;  //初始化当前用户的  sessionid
            httpContext.Application.Lock();
            httpContext.Application["Online"] = userOnline;
            httpContext.Application.UnLock();
        }
复制代码

添加验证特性类,自动让已经登录的用户下线:

复制代码
using System.Collections;

namespace System.Web.Mvc
{
    public  class SingleUserAuthorize:AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            Hashtable userOnline = (Hashtable)(httpContext.Application["Online"]);
            if(userOnline!=null)
            {
                IDictionaryEnumerator idE=userOnline.GetEnumerator();
                string strkey=string.Empty;
                if(userOnline.Count>0)
                {
                    while(idE.MoveNext())
                    {
                        //登录时判断保存的session是否与当前页面的session相同
                        if (userOnline.Contains(httpContext.Session.SessionID))
                        {
                            if (idE.Key != null && idE.Key.ToString().Equals(httpContext.Session.SessionID))
                            {
                                //判断当前session保存的值是否为被注销值
                                if (idE.Value != null && "XXXXXX".Equals(idE.Value.ToString()))
                                {
                                    //验证被注销则清空session
                                    userOnline.Remove(httpContext.Session.SessionID);
                                    httpContext.Application.Lock();
                                    httpContext.Application["Online"] = userOnline;
                                    httpContext.Response.Write("<script>alert('你的帐号在别处登录,你被强迫下线!');location.href='/Load';</script>");
                                    httpContext.Response.End();  
                                    return false;
                                }
                            }
                        }
                        else
                        {
                            return false;
                        }
                    }
                    return true;
                }
                else
                {
                    return false;
                }
            }
            return false; 
        }
    }
}
复制代码

验证的时候在控制器里添加:

[SingleUserAuthorize]

最后在Global.asax.cs里添加如下代码:

复制代码
//单点登录代码开始
        protected void Session_Start(object sender, EventArgs e) { }
        protected void Session_End(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();
            }
        }
        //单点登录代码结束
复制代码

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值