using System; using System.Collections.Generic; using System.Timers; using System.Web; using System.Web.Configuration; namespace CommonTool { /// /// Session 帮助类 /// public class SessionHelper { public static SessionHelper Instance { get; } = new SessionHelper(); public void AddSessionUser(T user,string sessionName= "SESSION_USER") { HttpContext rq = HttpContext.Current; rq.Session[sessionName] = user; } public T GetSessionUser(string sessionName = "SESSION_USER") { try { HttpContext rq = HttpContext.Current; return (T)rq.Session[sessionName]; } catch (Exception e) { throw new Exception(e.Message); } } public void Clear(string sessionName = "SESSION_USER") { HttpContext rq = HttpContext.Current; rq.Session[sessionName] = null; } /// /// 添加Session,调动有效期为20分钟 /// /// Session对象名称 /// Session值 public void AddSession(string strSessionName, string strValue) { AddSession(strSessionName, strValue, 20); } /// /// 添加Session /// /// Session对象名称 /// Session值 /// 调动有效期(分钟) public void AddSession(string strSessionName, string strValue, int iExpires) { if (HttpContext.Current.Session[strSessionName] != null) DelSession(strSessionName); HttpContext.Current.Session[strSessionName] = strValue; HttpContext.Current.Session.Timeout = iExpires; } /// /// 添加Session数组,调动有效期为20分钟 /// /// Session对象名称 /// Session值数组 public void AddSessions(string strSessionName, string[] strValues) { AddSessions(strSessionName, strValues, 20); } /// /// 添加Session数组 /// /// Session对象名称 /// Session值数组 /// 调动有效期(分钟) public void AddSessions(string strSessionName, string[] strValues, int iExpires) { if (HttpContext.Current.Session[strSessionName] != null) DelSession(strSessionName); HttpContext.Current.Session[strSessionName] = strValues; HttpContext.Current.Session.Timeout = iExpires; } /// /// 读取某个Session对象值 /// /// Session对象名称 /// Session对象值 public string GetSession(string strSessionName) { return HttpContext.Current.Session[strSessionName] == null ? null : HttpContext.Current.Session[strSessionName].ToString(); } /// /// 读取某个Session对象值数组 /// /// Session对象名称 /// Session对象值数组 public string[] GetSessions(string strSessionName) { return HttpContext.Current.Session[strSessionName] == null ? null : (string[]) HttpContext.Current.Session[strSessionName]; } /// /// 删除某个Session对象 /// /// Session对象名称 public void DelSession(string strSessionName) { HttpContext.Current.Session[strSessionName] = null; } } public class UserListSession { public UserListSession() { var timer = new Timer(5000); timer.Elapsed += myTimer_Elapsed; timer.Enabled = true; timer.AutoReset = true; } private void myTimer_Elapsed(object source, ElapsedEventArgs e) { List expireUser = new List(); foreach (string key in M.Keys) { SysUserInfo userinfo = M[key]; if (userinfo.IsExpire()) { expireUser.Add(userinfo); } } foreach (SysUserInfo t in expireUser) { M.Remove(t.UserGuid); LogHelper.Info(t.UserNo + "超时,被清空!"); } expireUser.Clear(); } public static UserListSession Instance = new UserListSession(); /// /// 检查用户的登录是否有效 /// /// public bool CheckLogin(UserType userType=UserType.GeneralUser) { bool lbRetval = false; string lcSessionName = WebConfigurationManager.AppSettings["UserInfoSession"]; if (userType!=UserType.GeneralUser) { lcSessionName = WebConfigurationManager.AppSettings["SysUserInfoSession"]; } string lcToken = SessionHelper.Instance.GetSession(lcSessionName); SessionHelper.Instance.DelSession(lcSessionName); if (string.IsNullOrEmpty(lcToken)) return false; if (CheckLogin(lcToken)) { SessionHelper.Instance.AddSession(lcSessionName, lcToken, 30); lbRetval = true; } return lbRetval; } /// /// 根据Token检查用户登录 /// /// Token /// public bool CheckLogin(string pcToken) { return Instance.IsContainsKey(pcToken); } public Dictionary M = new Dictionary(); public void Add(String guid, SysUserInfo poUserInfo) => M.Add(guid, poUserInfo); public SysUserInfo GetUserInfoBySession(string pcSessionName) { //string lcSessionName = WebConfigurationManager.AppSettings["UserInfoSession"]; string lcToken = SessionHelper.Instance.GetSession(pcSessionName); SessionHelper.Instance.DelSession(pcSessionName); if (string.IsNullOrEmpty(lcToken)) return null; SessionHelper.Instance.AddSession(pcSessionName, lcToken, 30); return GetUserInfoByGuid(lcToken); } public SysUserInfo GetUserInfoByGuid(String pcGuid) { if (IsContainsKey(pcGuid)) { return M[pcGuid]; } return null; } public bool IsContainsKey(String pcGuid) { if (M.ContainsKey(pcGuid)) { if (!M[pcGuid].IsExpire()) { M[pcGuid].RefreshTime(); return true; } M.Remove(pcGuid); return false; } return false; } } public class SysUserInfo { public SysUserInfo(string pcUserNo, string pcPassword, string pcUserName = "", UserType pcUserType = UserType.GeneralUser) { LastDateTime = DateTime.Now; UserNo = pcUserNo; Password = pcPassword; UserName = pcUserName; UserType = pcUserType; } public string UserNo { get; set; } public string Password { get; set; } public string UserName { get; set; } public UserType UserType { get; set; } public string UserGuid { get; set; } public DateTime LastDateTime { get; set; } public void RefreshTime() => LastDateTime = DateTime.Now; public bool IsExpire() => DateTime.Now > LastDateTime.AddHours(1); } public enum UserType { GeneralUser, SysUser, TeacherUser, SuperSysUser, Vip1, Vip2, Vip3, Vip4, Vip5, Vip6, Vip7 } }