我碰到你的遇到的问题,当时真的是很棘手,不过现在解决了希望能帮到你。首先,上面几楼说的方法我都有试过用Cookie保存,和用session一样的,同样会出现一样的情况。如果用一个字段来标识登录状态1或0,非法退出断电情况都会导致更新不了字段,从而锁死用户。 在登录页面,一切判断做好后,判断session["user"]==null;然后贴上代码 ArrayList list = Application.Get("GLOBAL_USER_LIST") as ArrayList; if (list == null) { list = new ArrayList(); } for (int i = 0; i < list.Count; i++) { if (user.UserId == (list[i] as string)) { //已经登录了,提示错误信息 Page.RegisterStartupScript("aa", "<script>alert('您无法登录,该用户名已经有人登录了!')</script>"); return; } }检查你的用户是否登录了。 然后在退出的时候去移除这个用户,贴上代码(不知道你的项目退出怎么设计的,如果有专门的安全退出按钮就调用这个方法): public void clearsession() { if(user != null) { string strUserId = user.UserId; ArrayList list = Application.Get("GLOBAL_USER_LIST") as ArrayList; if (strUserId != null && list != null) { list.Remove(strUserId); Application.Add("GLOBAL_USER_LIST", list); } } } 然后再来扑捉直接关闭浏览器,任务栏下面的右键关闭:(不知道你采用的是什么布局)在你的母版页或是框架主页上面贴上js代码: function window.onbeforeunload() { //用户点击浏览器右上角关闭按钮或是按alt+F4关闭
HttpCookie objCookie = new HttpCookie("SysUser"); //定义cookie对象以及名为Info的项
objCookie.Domain = ".xxx.com"; //定义cookie对象Domain,作用域
DateTime dt = DateTime.Now; //定义时间对象
TimeSpan ts = new TimeSpan(1, 0, 0, 0); //cookie有效作用时间,具体查msdn
objCookie.Expires = dt.Add(ts); //添加作用时间
objCookie.Values.Add("Account", returnModel.SU_AccountID); //增加属性
objCookie.Values.Add("UserID", returnModel.SU_ID.ToString());
在登录页面,一切判断做好后,判断session["user"]==null;然后贴上代码 ArrayList list = Application.Get("GLOBAL_USER_LIST") as ArrayList;
if (list == null)
{
list = new ArrayList();
}
for (int i = 0; i < list.Count; i++)
{
if (user.UserId == (list[i] as string))
{
//已经登录了,提示错误信息
Page.RegisterStartupScript("aa", "<script>alert('您无法登录,该用户名已经有人登录了!')</script>");
return; }
}检查你的用户是否登录了。
然后在退出的时候去移除这个用户,贴上代码(不知道你的项目退出怎么设计的,如果有专门的安全退出按钮就调用这个方法):
public void clearsession()
{
if(user != null)
{
string strUserId = user.UserId;
ArrayList list = Application.Get("GLOBAL_USER_LIST") as ArrayList;
if (strUserId != null && list != null)
{
list.Remove(strUserId);
Application.Add("GLOBAL_USER_LIST", list); }
}
}
然后再来扑捉直接关闭浏览器,任务栏下面的右键关闭:(不知道你采用的是什么布局)在你的母版页或是框架主页上面贴上js代码:
function window.onbeforeunload()
{
//用户点击浏览器右上角关闭按钮或是按alt+F4关闭
var n=window.event.screenX-window.screenLeft;
var b=n>document.documentElement.scrollWidth-20; if(b&&window.event.clientY<0||window.event.altKey) // alert("点关闭按钮"); window.event.returnValue="确定要退出本页吗?";
{
InsertSQL();
} //用户点击任务栏,右键关闭。s或是按alt+F4关闭
else if(event.clientY > document.body.clientHeight || event.altKey)
{
InsertSQL();
}
//其他情况为刷新
else
{
} }
function InsertSQL()//(ajax)
{
var Http = new ActiveXObject("Microsoft.XMLHTTP");
Http.open("GET","InsertExitLog.aspx",false); //检查Session失效的页面
Http.send(null);
var str = Http.responseText;//执行ASPX后的返回结果
}
而InsertExitLog.aspx这个页面里面的代码,也同样是上面clearsession()
这个方法,不过InsertExitLog.aspx的源里面的代码只要<%@ Page Language="C#" AutoEventWireup="true" CodeFile="InsertExitLog.aspx.cs" Inherits="InsertExitLog" %>
这个即可,其他的标签都删掉。
如果是怕断电,用户没有通过上面的方法移除,那么要等到session过期才可以,你可以创建一个Global.asax文件(在选中网站添加一个“全局应用程序类”)里面的代码只要到void Session_End(object sender, EventArgs e) 这个里面加上clearsession()
这个方法里面的方法
再到web.config里面</pages>这个下面加入 <sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes" cookieless="false" timeout="1"/>表示当非法退出过1分钟(实际差不多是快2分钟)后自动清除刚刚的用户。同时为了保证你的session在1分钟内不操作而不被失效,就需要在你的ie里面设置一下,Internet选项---常规---设置---在检查所访问网页下选择第一个“每次访问网页时”。我的方法是这个,如果你觉得麻烦可以另寻其他方法。