要防止同一用户同时登录,首页应该记录在线用户的信息(这里与用户名为例),而后判断正在登录的用户里面是否已存在。在这里使用一个cache存放已经登录的用户名,可是还有一个问题就是要知道用户是何时离开系统的呢?这就要按期清除cache中的内容了,也就是设置一个cache的时间。这个时间能够跟用户的session值联系起来,恰好当用户session值失效的时候该用户在cache里面的信息也会被清空.这样就达到了防止同时登录的效果。html
具体代码以下:session
string key = TextBox1.Text; //用户名文本框设为cache关键字 string uer = Convert.ToString(Cache[key]); //读取cache中用户相应的值 //判断cache中是否有用户的信息,若是没有相关的值,说明用户未登录 if (uer == null || uer == String.Empty) { //定义cache过时时间 TimeSpan SessTimeout = new TimeSpan(0, 0, System.Web.HttpContext.Current.Session.Timeout, 0, 0); //第一次登录的时候插入一个用户相关的cache值, HttpContext.Current.Cache.Insert(key, key, null, DateTime.MaxValue, SessTimeout, System.Web.Caching.CacheItemPriority.NotRemovable, null); Session["ADMINID"] = TextBox1.Text; Response.Redirect("main.aspx"); } else { //重复登录 Response.Write("<script>alert('您的帐号已经登录!');window.location='login.aspx';</script>"); }
★转载请注明出处:http://www.cnblogs.com/zfanlong1314/archive/2012/01/17/2390457.htmlspa