如何同一时间一个帐号只有一个用户使用

来源:互联网 发布:主角与配角 知乎 编辑:程序博客网 时间:2024/06/16 00:16
asp.net有个eventhandle是PreRequestHandlerExecute,好了,我们就在这个事件里面判用户是否已经被其他人登录了,如果是,则转向到提示页面吧。
//当前Session是否存在
if(System.Web.HttpContext.Current.Session!=null)
{
//用户是否登录
if(System.Web.HttpContext.Current.Session["UserId"]!=null)
{
//获取用户登录的SessionID,在登录页面设置,并判定两个ID是否是相同的,不同嘛则后来有人用你的帐号登录了。
if(Application["Session"+System.Web.HttpContext.Current.Session["UserId"].ToString()].ToString() !=System.Web.HttpContext.Current.Session.SessionID)
{
Response.Redirect("/otheroneLogin.html",true);
}
}
}
//在初始化Global中Hook
public Global()
{
this.PreRequestHandlerExecute += new System.EventHandler(this.Global_PreRequestHandlerExecute);
}
//登录时设定的代码
Application.Lock();
Application["Session"+Session["UserId"].ToString()]=Session.SessionID;
Application.UnLock();