SessionHelper.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Timers;
  4. using System.Web;
  5. using System.Web.Configuration;
  6. namespace CommonTool
  7. {
  8. /// <summary>
  9. /// Session 帮助类
  10. /// </summary>
  11. public class SessionHelper
  12. {
  13. public static SessionHelper Instance { get; } = new SessionHelper();
  14. public void AddSessionUser<T>(T user,string sessionName= "SESSION_USER")
  15. {
  16. HttpContext rq = HttpContext.Current;
  17. rq.Session[sessionName] = user;
  18. }
  19. public T GetSessionUser<T>(string sessionName = "SESSION_USER")
  20. {
  21. try
  22. {
  23. HttpContext rq = HttpContext.Current;
  24. return (T)rq.Session[sessionName];
  25. }
  26. catch (Exception e)
  27. {
  28. throw new Exception(e.Message);
  29. }
  30. }
  31. public void Clear(string sessionName = "SESSION_USER")
  32. {
  33. HttpContext rq = HttpContext.Current;
  34. rq.Session[sessionName] = null;
  35. }
  36. /// <summary>
  37. /// 添加Session,调动有效期为20分钟
  38. /// </summary>
  39. /// <param name="strSessionName">Session对象名称</param>
  40. /// <param name="strValue">Session值</param>
  41. public void AddSession(string strSessionName, string strValue)
  42. {
  43. AddSession(strSessionName, strValue, 20);
  44. }
  45. /// <summary>
  46. /// 添加Session
  47. /// </summary>
  48. /// <param name="strSessionName">Session对象名称</param>
  49. /// <param name="strValue">Session值</param>
  50. /// <param name="iExpires">调动有效期(分钟)</param>
  51. public void AddSession(string strSessionName, string strValue, int iExpires)
  52. {
  53. if (HttpContext.Current.Session[strSessionName] != null)
  54. DelSession(strSessionName);
  55. HttpContext.Current.Session[strSessionName] = strValue;
  56. HttpContext.Current.Session.Timeout = iExpires;
  57. }
  58. /// <summary>
  59. /// 添加Session数组,调动有效期为20分钟
  60. /// </summary>
  61. /// <param name="strSessionName">Session对象名称</param>
  62. /// <param name="strValues">Session值数组</param>
  63. public void AddSessions(string strSessionName, string[] strValues)
  64. {
  65. AddSessions(strSessionName, strValues, 20);
  66. }
  67. /// <summary>
  68. /// 添加Session数组
  69. /// </summary>
  70. /// <param name="strSessionName">Session对象名称</param>
  71. /// <param name="strValues">Session值数组</param>
  72. /// <param name="iExpires">调动有效期(分钟)</param>
  73. public void AddSessions(string strSessionName, string[] strValues, int iExpires)
  74. {
  75. if (HttpContext.Current.Session[strSessionName] != null)
  76. DelSession(strSessionName);
  77. HttpContext.Current.Session[strSessionName] = strValues;
  78. HttpContext.Current.Session.Timeout = iExpires;
  79. }
  80. /// <summary>
  81. /// 读取某个Session对象值
  82. /// </summary>
  83. /// <param name="strSessionName">Session对象名称</param>
  84. /// <returns>Session对象值</returns>
  85. public string GetSession(string strSessionName)
  86. {
  87. return HttpContext.Current.Session[strSessionName] == null
  88. ? null
  89. : HttpContext.Current.Session[strSessionName].ToString();
  90. }
  91. /// <summary>
  92. /// 读取某个Session对象值数组
  93. /// </summary>
  94. /// <param name="strSessionName">Session对象名称</param>
  95. /// <returns>Session对象值数组</returns>
  96. public string[] GetSessions(string strSessionName)
  97. {
  98. return HttpContext.Current.Session[strSessionName] == null
  99. ? null
  100. : (string[]) HttpContext.Current.Session[strSessionName];
  101. }
  102. /// <summary>
  103. /// 删除某个Session对象
  104. /// </summary>
  105. /// <param name="strSessionName">Session对象名称</param>
  106. public void DelSession(string strSessionName)
  107. {
  108. HttpContext.Current.Session[strSessionName] = null;
  109. }
  110. }
  111. public class UserListSession
  112. {
  113. public UserListSession()
  114. {
  115. var timer = new Timer(5000);
  116. timer.Elapsed += myTimer_Elapsed;
  117. timer.Enabled = true;
  118. timer.AutoReset = true;
  119. }
  120. private void myTimer_Elapsed(object source, ElapsedEventArgs e)
  121. {
  122. List<SysUserInfo> expireUser = new List<SysUserInfo>();
  123. foreach (string key in M.Keys)
  124. {
  125. SysUserInfo userinfo = M[key];
  126. if (userinfo.IsExpire())
  127. {
  128. expireUser.Add(userinfo);
  129. }
  130. }
  131. foreach (SysUserInfo t in expireUser)
  132. {
  133. M.Remove(t.UserGuid);
  134. LogHelper.Info(t.UserNo + "超时,被清空!");
  135. }
  136. expireUser.Clear();
  137. }
  138. public static UserListSession Instance = new UserListSession();
  139. /// <summary>
  140. /// 检查用户的登录是否有效
  141. /// </summary>
  142. /// <returns></returns>
  143. public bool CheckLogin(UserType userType=UserType.GeneralUser)
  144. {
  145. bool lbRetval = false;
  146. string lcSessionName = WebConfigurationManager.AppSettings["UserInfoSession"];
  147. if (userType!=UserType.GeneralUser)
  148. {
  149. lcSessionName = WebConfigurationManager.AppSettings["SysUserInfoSession"];
  150. }
  151. string lcToken = SessionHelper.Instance.GetSession(lcSessionName);
  152. SessionHelper.Instance.DelSession(lcSessionName);
  153. if (string.IsNullOrEmpty(lcToken))
  154. return false;
  155. if (CheckLogin(lcToken))
  156. {
  157. SessionHelper.Instance.AddSession(lcSessionName, lcToken, 30);
  158. lbRetval = true;
  159. }
  160. return lbRetval;
  161. }
  162. /// <summary>
  163. /// 根据Token检查用户登录
  164. /// </summary>
  165. /// <param name="pcToken">Token</param>
  166. /// <returns></returns>
  167. public bool CheckLogin(string pcToken)
  168. {
  169. return Instance.IsContainsKey(pcToken);
  170. }
  171. public Dictionary<string, SysUserInfo> M = new Dictionary<string, SysUserInfo>();
  172. public void Add(String guid, SysUserInfo poUserInfo) => M.Add(guid, poUserInfo);
  173. public SysUserInfo GetUserInfoBySession(string pcSessionName)
  174. {
  175. //string lcSessionName = WebConfigurationManager.AppSettings["UserInfoSession"];
  176. string lcToken = SessionHelper.Instance.GetSession(pcSessionName);
  177. SessionHelper.Instance.DelSession(pcSessionName);
  178. if (string.IsNullOrEmpty(lcToken))
  179. return null;
  180. SessionHelper.Instance.AddSession(pcSessionName, lcToken, 30);
  181. return GetUserInfoByGuid(lcToken);
  182. }
  183. public SysUserInfo GetUserInfoByGuid(String pcGuid)
  184. {
  185. if (IsContainsKey(pcGuid))
  186. {
  187. return M[pcGuid];
  188. }
  189. return null;
  190. }
  191. public bool IsContainsKey(String pcGuid)
  192. {
  193. if (M.ContainsKey(pcGuid))
  194. {
  195. if (!M[pcGuid].IsExpire())
  196. {
  197. M[pcGuid].RefreshTime();
  198. return true;
  199. }
  200. M.Remove(pcGuid);
  201. return false;
  202. }
  203. return false;
  204. }
  205. }
  206. public class SysUserInfo
  207. {
  208. public SysUserInfo(string pcUserNo, string pcPassword, string pcUserName = "", UserType pcUserType = UserType.GeneralUser)
  209. {
  210. LastDateTime = DateTime.Now;
  211. UserNo = pcUserNo;
  212. Password = pcPassword;
  213. UserName = pcUserName;
  214. UserType = pcUserType;
  215. }
  216. public string UserNo { get; set; }
  217. public string Password { get; set; }
  218. public string UserName { get; set; }
  219. public UserType UserType { get; set; }
  220. public string UserGuid { get; set; }
  221. public DateTime LastDateTime { get; set; }
  222. public void RefreshTime() => LastDateTime = DateTime.Now;
  223. public bool IsExpire() => DateTime.Now > LastDateTime.AddHours(1);
  224. }
  225. public enum UserType
  226. {
  227. GeneralUser,
  228. SysUser,
  229. TeacherUser,
  230. SuperSysUser,
  231. Vip1,
  232. Vip2,
  233. Vip3,
  234. Vip4,
  235. Vip5,
  236. Vip6,
  237. Vip7
  238. }
  239. }