| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- using System;
- using System.Collections.Generic;
- using System.Timers;
- using System.Web;
- using System.Web.Configuration;
- namespace CommonTool
- {
- /// <summary>
- /// Session 帮助类
- /// </summary>
- public class SessionHelper
- {
- public static SessionHelper Instance { get; } = new SessionHelper();
- public void AddSessionUser<T>(T user,string sessionName= "SESSION_USER")
- {
- HttpContext rq = HttpContext.Current;
- rq.Session[sessionName] = user;
- }
- public T GetSessionUser<T>(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;
- }
-
- /// <summary>
- /// 添加Session,调动有效期为20分钟
- /// </summary>
- /// <param name="strSessionName">Session对象名称</param>
- /// <param name="strValue">Session值</param>
- public void AddSession(string strSessionName, string strValue)
- {
- AddSession(strSessionName, strValue, 20);
- }
- /// <summary>
- /// 添加Session
- /// </summary>
- /// <param name="strSessionName">Session对象名称</param>
- /// <param name="strValue">Session值</param>
- /// <param name="iExpires">调动有效期(分钟)</param>
- 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;
- }
- /// <summary>
- /// 添加Session数组,调动有效期为20分钟
- /// </summary>
- /// <param name="strSessionName">Session对象名称</param>
- /// <param name="strValues">Session值数组</param>
- public void AddSessions(string strSessionName, string[] strValues)
- {
- AddSessions(strSessionName, strValues, 20);
- }
- /// <summary>
- /// 添加Session数组
- /// </summary>
- /// <param name="strSessionName">Session对象名称</param>
- /// <param name="strValues">Session值数组</param>
- /// <param name="iExpires">调动有效期(分钟)</param>
- 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;
- }
- /// <summary>
- /// 读取某个Session对象值
- /// </summary>
- /// <param name="strSessionName">Session对象名称</param>
- /// <returns>Session对象值</returns>
- public string GetSession(string strSessionName)
- {
- return HttpContext.Current.Session[strSessionName] == null
- ? null
- : HttpContext.Current.Session[strSessionName].ToString();
- }
- /// <summary>
- /// 读取某个Session对象值数组
- /// </summary>
- /// <param name="strSessionName">Session对象名称</param>
- /// <returns>Session对象值数组</returns>
- public string[] GetSessions(string strSessionName)
- {
- return HttpContext.Current.Session[strSessionName] == null
- ? null
- : (string[]) HttpContext.Current.Session[strSessionName];
- }
- /// <summary>
- /// 删除某个Session对象
- /// </summary>
- /// <param name="strSessionName">Session对象名称</param>
- 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<SysUserInfo> expireUser = new List<SysUserInfo>();
- 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();
- /// <summary>
- /// 检查用户的登录是否有效
- /// </summary>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 根据Token检查用户登录
- /// </summary>
- /// <param name="pcToken">Token</param>
- /// <returns></returns>
- public bool CheckLogin(string pcToken)
- {
- return Instance.IsContainsKey(pcToken);
- }
- public Dictionary<string, SysUserInfo> M = new Dictionary<string, SysUserInfo>();
- 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
- }
- }
|