/// <summary> /// 快速直接注销 /// </summary> private void Fast_Loginoff() { //直接返回参数不对 if (g_find_user == null || g_find_user.Trim() == "") { return; } string ulist = ""; try { int count = 0; IntPtr buffer = IntPtr.Zero; StringBuilder userId = new StringBuilder(); //提取会话信息 TSControl.WTS_SESSION_INFO[] pSessionInfo = TSControl.SessionEnumeration(); for (int i = 0; i < pSessionInfo.Length; i++) { bool bsuccess = TSControl.WTSQuerySessionInformation(IntPtr.Zero, pSessionInfo[i].SessionID, TSControl.WTSInfoClass.WTSUserName, out userId, out count); if (bsuccess && userId.ToString().Trim() != "") { //AddLog("va:"+userId.ToString().Trim()); //不符号就注销 if (userId.ToString().Trim().ToLower() != g_find_user.Trim().ToLower()) { ulist += userId.ToString().Trim() + ","; //直接注销 sys_Logoff(pSessionInfo[i].SessionID); } } //AddLog("va:" + userId.ToString().Trim() + pSessionInfo.Length.ToString()); } } catch (Exception ex) { AddLog("get_LoginUser异常,原因:" + ex.Message); } try { if (ulist.Trim() != "") { if (ulist.EndsWith(",")) { ulist = ulist.Substring(0, ulist.Length - 1); } string smsMemo = "发现新登陆用户:[" + ulist + "]时间:" + DateTime.Now.ToString(); //发送短信 SendSMS(smsMemo); } } catch (Exception ex) { AddLog("get_LoginUser_短信发送异常,原因:" + ex.Message); } }
//多个用户之间用|分开必须显示用户和会话ID.可以终端非法用户 private string get_LoginUser() { string ret = ""; //清空 al_session.Clear(); try { int count = 0; IntPtr buffer = IntPtr.Zero; StringBuilder userId = new StringBuilder(); //提取会话信息 TSControl.WTS_SESSION_INFO[] pSessionInfo = TSControl.SessionEnumeration(); for (int i = 0; i < pSessionInfo.Length; i++) { bool bsuccess = TSControl.WTSQuerySessionInformation(IntPtr.Zero, pSessionInfo[i].SessionID, TSControl.WTSInfoClass.WTSUserName, out userId, out count); if (bsuccess && userId.ToString().Trim() != "") { LoginUser lu = new LoginUser(); lu.RunStatus = pSessionInfo[i].state.ToString(); lu.UserId = userId.ToString().Trim(); lu.SessionId = pSessionInfo[i].SessionID; lu.SessionName = pSessionInfo[i].pWinStationName; //加入会话记录 al_session.Add(lu); //非授权用户,记录所有登陆到本机器的账号名 ret += lu.UserId.ToString().Trim() + ","; } } } catch (Exception ex) { AddLog("get_LoginUser异常,原因:" + ex.Message); } if (ret.EndsWith(",")) { ret = ret.Substring(0, ret.Length - 1); } return(ret); }
/// <summary> /// 注销会话ID-必须注销 /// </summary> /// <param name="nSessionId"></param> private void sys_Logoff(int nSessionId) { try { try { //立即注销 TSControl.WTSLogoffSession(0, nSessionId, false); } catch { } //二次注销 GetCmd("logoff", nSessionId.ToString()); //记录注销信息 AddLog("会话ID:" + nSessionId.ToString() + "被注销."); //发送注销短信 SendSMS("会话ID:" + nSessionId.ToString() + "执行注销.时间:" + DateTime.Now.ToString()); } catch (Exception ex) { AddLog("注销会员:" + nSessionId.ToString() + "异常,原因:" + ex.Message); } }