protected void Session_Start(object sender, EventArgs e)
{ }
protected void Session_End(object sender, EventArgs e)
{
Hashtable hOnline = (Hashtable)Application["Online"];
if (hOnline != null)
{
if (hOnline[Session.SessionID] != null)
{
hOnline.Remove(Session.SessionID);
Application.Lock();
Application["Online"] = hOnline;
Application.UnLock();
}
}
}
#endregion
//每次用户登陆时均判断以下要登陆的用户名在Online中是不是已经存在,如果存在该用户名已经被登陆,将第一个人登陆的SessionID对应的用户名强制变更为_offline_,表示该登陆将被强制注销。
//需要Global.cs里添加Session_Start和Session_End方法,保证同一次会话的SessionID不变
private void HandleApplication(int userID)
{
HttpContext httpContext = System.Web.HttpContext.Current;
Hashtable userOnline = (Hashtable)httpContext.Application["Online"];
if (userOnline != null)
{
IDictionaryEnumerator idE = userOnline.GetEnumerator();
string strKey = string.Empty;
while (idE.MoveNext())
{
if (idE.Value != null && idE.Value.ToString().Equals(userID.ToString()))
{
strKey = idE.Key.ToString();
userOnline[strKey] = "_offline_";
break;
}
}
}
else
{
userOnline = new Hashtable();
}
userOnline[Session.SessionID] = userID.ToString();
httpContext.Application.Lock();
httpContext.Application["Online"] = userOnline;
httpContext.Application.UnLock();
}
//定时检测是否被强制下线
$(document).ready(function () {
//是否是正式环境
if (isProduction == "true") {
setInterval(function () {
CheckIsForcedLogout();
}, 10000);
}
});
//检测是否被强制下线
function CheckIsForcedLogout() {
$.ajax({
url: "/Home/CheckIsForcedLogout",
type: "POST",
dataType: "json",
success: function (msg) {
if (msg.OperateResult == "Success") {
$.messager.alert('', msg.OperateData, 'error', function () {
window.location.href = "/Account/Login";
});
}
},
error: function (ex) { }
});
}
/// <summary>
/// 检测是否被强制下线
/// </summary>
/// <returns></returns>
[HttpPost]
public JsonResult CheckIsForcedLogout()
{
try
{
HttpContext httpContext = System.Web.HttpContext.Current;
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是否与当前页面的sesion相同
if (userOnline.Contains(httpContext.Session.SessionID))
{
if (idE.Key != null && idE.Key.ToString().Equals(httpContext.Session.SessionID))
{
//判断当前session保存的值是否为被注销值
if (idE.Value != null && "_offline_".Equals(idE.Value.ToString()))
{
//验证被注销则清空session
userOnline.Remove(httpContext.Session.SessionID);
httpContext.Application.Lock();
httpContext.Application["online"] = userOnline;
httpContext.Application.UnLock();
string msg = string.Format("下线通知:当前账号于 {0} 在另一地点 {1} 登录, 您被迫下线。若非本人操作,您的登录密码很可能已经泄露,请及时改密。",
currentUser.LastLoginTime.ToString("yyyy-MM-dd HH:mm:ss"), currentUser.LastLoginIp);
//登出,清除cookie
FormsAuthentication.SignOut();
return Json(new { OperateResult = ConstantDim.operateSuccess, OperateData = msg }, JsonRequestBehavior.AllowGet);
}
}
}
}
}
}
return Json(new { OperateResult = ConstantDim.operateFailed }, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
return Json(new { OperateResult = ConstantDim.operateFailed }, JsonRequestBehavior.AllowGet);
}
}