要防止同一用户同时登陆,首页应该记录在线用户的信息(这里与用户名为例),然后判断正在登陆的用户里面是否已存在。在这里使用一个cache存放已经登陆的用户名,但是还有一个问题就是要知道用户是什么时候离开系统的呢?这就要定期清除cache中的内容了,也就是设置一个cache的时间。这个时间可以跟用户的session值联系起来,刚好当用户session值失效的时候该用户在cache里面的信息也会被清空.这样就达到了防止同时登陆的效果,具体代码如下:
放在登陆成功的地方:
 1![]() string key = TextBox1.Text; //用户名文本框设为cache关键字
string key = TextBox1.Text; //用户名文本框设为cache关键字 
2![]() string uer = Convert.ToString(Cache[key]); //读取cache中用户相应的值
string uer = Convert.ToString(Cache[key]); //读取cache中用户相应的值
3![]() //判断cache中是否有用户的信息,如果没有相关的值,说明用户未登陆
//判断cache中是否有用户的信息,如果没有相关的值,说明用户未登陆
4![]() if (uer == null || uer == String.Empty)
if (uer == null || uer == String.Empty)
5![]() {
{
6![]() //定义cache过期时间
 //定义cache过期时间
7![]()
8![]() TimeSpan SessTimeout = new TimeSpan(0, 0, System.Web.HttpContext.Current.Session.Timeout, 0, 0);
 TimeSpan SessTimeout = new TimeSpan(0, 0, System.Web.HttpContext.Current.Session.Timeout, 0, 0);
9![]()
10![]() //第一次登陆的时候插入一个用户相关的cache值,
 //第一次登陆的时候插入一个用户相关的cache值,
11![]() HttpContext.Current.Cache.Insert(key, key, null, DateTime.MaxValue, SessTimeout, System.Web.Caching.CacheItemPriority.NotRemovable, null);
 HttpContext.Current.Cache.Insert(key, key, null, DateTime.MaxValue, SessTimeout, System.Web.Caching.CacheItemPriority.NotRemovable, null);
12![]() Session["ADMINID"] = TextBox1.Text;
 Session["ADMINID"] = TextBox1.Text;
13![]() Response.Redirect("main.aspx");
 Response.Redirect("main.aspx");
14![]() }
}
15![]() else
else
16![]() {
{
17![]() //重复登陆
 //重复登陆
18![]() Response.Write("<script>alert('您的账号已经登陆!');window.location='login.aspx';</script>");
 Response.Write("<script>alert('您的账号已经登陆!');window.location='login.aspx';</script>");
19![]() }
}
 string key = TextBox1.Text; //用户名文本框设为cache关键字
string key = TextBox1.Text; //用户名文本框设为cache关键字 2
 string uer = Convert.ToString(Cache[key]); //读取cache中用户相应的值
string uer = Convert.ToString(Cache[key]); //读取cache中用户相应的值3
 //判断cache中是否有用户的信息,如果没有相关的值,说明用户未登陆
//判断cache中是否有用户的信息,如果没有相关的值,说明用户未登陆4
 if (uer == null || uer == String.Empty)
if (uer == null || uer == String.Empty)5
 {
{6
 //定义cache过期时间
 //定义cache过期时间7

8
 TimeSpan SessTimeout = new TimeSpan(0, 0, System.Web.HttpContext.Current.Session.Timeout, 0, 0);
 TimeSpan SessTimeout = new TimeSpan(0, 0, System.Web.HttpContext.Current.Session.Timeout, 0, 0);9

10
 //第一次登陆的时候插入一个用户相关的cache值,
 //第一次登陆的时候插入一个用户相关的cache值,11
 HttpContext.Current.Cache.Insert(key, key, null, DateTime.MaxValue, SessTimeout, System.Web.Caching.CacheItemPriority.NotRemovable, null);
 HttpContext.Current.Cache.Insert(key, key, null, DateTime.MaxValue, SessTimeout, System.Web.Caching.CacheItemPriority.NotRemovable, null);12
 Session["ADMINID"] = TextBox1.Text;
 Session["ADMINID"] = TextBox1.Text;13
 Response.Redirect("main.aspx");
 Response.Redirect("main.aspx");14
 }
}15
 else
else16
 {
{17
 //重复登陆
 //重复登陆18
 Response.Write("<script>alert('您的账号已经登陆!');window.location='login.aspx';</script>");
 Response.Write("<script>alert('您的账号已经登陆!');window.location='login.aspx';</script>");19
 }
} 
                    
                 


 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号