实现自定义Session
1. 回话状态接口
/// <summary> /// 会话状态策略接口 /// </summary> public partial interface ISessionStrategy { /// <summary> /// 过期时间(单位为秒) /// </summary> int Timeout { get; } /// <summary> /// 获得用户会话状态数据 /// </summary> /// <param name="sid">sid</param> /// <returns>Dictionary<string,object>类型</returns> Dictionary<string,object> GetSession(string sid); /// <summary> /// 移除用户会话状态数据 /// </summary> /// <param name="sid">sid</param> void RemoverSession(string sid); /// <summary> /// 获得用户会话状态数据的数据项的值 /// </summary> /// <param name="sid">sid</param> /// <param name="key">键</param> /// <returns>当前键值不存在时返回null</returns> object GetValue(string sid, string key); /// <summary> /// 设置用户会话状态数据的数据项 /// </summary> /// <param name="sid">sid</param> /// <param name="key">键</param> /// <param name="value">值</param> void SetItem(string sid, string key, object value); /// <summary> /// 移除用户会话状态数据的数据项 /// </summary> /// <param name="sid">sid</param> /// <param name="key">键</param> void RemoveItem(string sid, string key); }
2.接口实现
/// <summary> /// 基于Asp.Net缓存的会话状态策略 /// </summary> public partial class SessionStrategy : ISessionStrategy { private Cache _cache;//Asp.Net缓存 private int _timeout = 600;//过期时间(单位为秒) public SessionStrategy() { _cache = HttpRuntime.Cache; } /// <summary> /// 过期时间(单位为秒) /// </summary> /// <value></value> public int Timeout { get { return _timeout; } } /// <summary> /// 获得用户会话状态数据 /// </summary> /// <param name="sid">sid</param> /// <returns>Dictionary<string,object>类型</returns> public Dictionary<string, object> GetSession(string sid) { object session = _cache.Get(sid); if (session != null) return (Dictionary<string, object>)session; Dictionary<string, object> s = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase); _cache.Insert(sid, s, null, DateTime.Now.AddSeconds(_timeout), Cache.NoSlidingExpiration, CacheItemPriority.High, null); return s; } /// <summary> /// 移除用户会话状态数据 /// </summary> /// <param name="sid">sid</param> public void RemoverSession(string sid) { _cache.Remove(sid); } /// <summary> /// 获得用户会话状态数据的数据项的值 /// </summary> /// <param name="sid">sid</param> /// <param name="key">键</param> /// <returns>当前键值不存在时返回null</returns> public object GetValue(string sid, string key) { object session = _cache.Get(sid); if (session == null) { return null; } else { Dictionary<string, object> s = (Dictionary<string, object>)session; object value = null; s.TryGetValue(key, out value); return value; } } /// <summary> /// 设置用户会话状态数据的数据项 /// </summary> /// <param name="sid">sid</param> /// <param name="key">键</param> /// <param name="value">值</param> public void SetItem(string sid, string key, object value) { Dictionary<string, object> session = GetSession(sid); if (session.ContainsKey(key)) session[key] = value; else session.Add(key, value); } /// <summary> /// 移除用户会话状态数据的数据项 /// </summary> /// <param name="sid">sid</param> /// <param name="key">键</param> public void RemoveItem(string sid, string key) { object session = _cache.Get(sid); if (session != null) { Dictionary<string, object> s = (Dictionary<string, object>)session; s.Remove(key); } } }
3.服务层调用
/// <summary> /// 会话状态操作管理类 /// </summary> public partial class Sessions { private static ISessionStrategy _isessionstrategy = null;//会话状态策略 static Sessions() { _isessionstrategy = BMASession.Instance; } /// <summary> /// 生成sessionid /// </summary> /// <returns></returns> public static string GenerateSid() { long i = 1; byte[] byteArray = Guid.NewGuid().ToByteArray(); foreach (byte b in byteArray) { i *= ((int)b + 1); } return string.Format("{0:x}", i - DateTime.Now.Ticks); } /// <summary> /// 过期时间(单位为秒) /// </summary> /// <value></value> public static int Timeout { get { return _isessionstrategy.Timeout; } } /// <summary> /// 获得用户会话状态数据 /// </summary> /// <param name="sid">sid</param> /// <returns>Dictionary<string,object>类型</returns> public static Dictionary<string, object> GetSession(string sid) { return _isessionstrategy.GetSession(sid); } /// <summary> /// 移除用户会话状态数据 /// </summary> /// <param name="sid">sid</param> public static void RemoverSession(string sid) { _isessionstrategy.RemoverSession(sid); } /// <summary> /// 获得用户会话状态数据的数据项的值 /// </summary> /// <param name="sid">sid</param> /// <param name="key">键</param> /// <returns>当前键值不存在时返回null</returns> public static object GetValue(string sid, string key) { if (!string.IsNullOrEmpty(key)) return _isessionstrategy.GetValue(sid, key); else return null; } /// <summary> /// 获得用户会话状态数据的数据项的值 /// </summary> /// <param name="sid">sid</param> /// <param name="key">键</param> /// <returns></returns> public static int GetValueInt(string sid, string key) { return TypeHelper.ObjectToInt(GetValue(sid, key)); } /// <summary> /// 获得用户会话状态数据的数据项的值 /// </summary> /// <param name="sid">sid</param> /// <param name="key">键</param> /// <returns></returns> public static string GetValueString(string sid, string key) { object value = GetValue(sid, key); if (value != null) return value.ToString(); else return string.Empty; } /// <summary> /// 设置用户会话状态数据的数据项 /// </summary> /// <param name="sid">sid</param> /// <param name="key">键</param> /// <param name="value">值</param> public static void SetItem(string sid, string key, object value) { if (!string.IsNullOrEmpty(key)) _isessionstrategy.SetItem(sid, key, value); } /// <summary> /// 移除用户会话状态数据的数据项 /// </summary> /// <param name="sid">sid</param> /// <param name="key">键</param> public static void RemoveItem(string sid, string key) { if (!string.IsNullOrEmpty(key)) _isessionstrategy.RemoveItem(sid, key); } } /// <summary> /// session键类 /// </summary> public partial class SessionKey { /// <summary> /// 图片验证码 /// </summary> public const string VERIFYIMAGEVALUE = "verifyImageValue"; /// <summary> /// 安全中心手机验证值 /// </summary> public const string SAFEVERIFYMOBILEVALUE = "safeVerifyMoibleValue"; /// <summary> /// 安全中心更新手机值 /// </summary> public const string UPDATEMOBILEVALUE = "updateMoibleValue"; /// <summary> /// 安全中心更新手机 /// </summary> public const string UPDATEMOBILE = "updateMoible"; /// <summary> /// 安全中心验证密码动作 /// </summary> public const string VALIDATEPASSWORD = "validatePassword"; /// <summary> /// 安全中心验证手机动作 /// </summary> public const string VALIDATEMOBILE = "validateMobile"; /// <summary> /// 安全中心验证邮箱动作 /// </summary> public const string VALIDATEEMAIL = "validateEmail"; }
4. 简洁版:
/// <summary> /// 基于Asp.Net缓存的会话状态策略 /// </summary> public partial class SessionStrategy { private Cache _cache;//Asp.Net缓存 private int _timeout = 600;//过期时间(单位为秒) public SessionStrategy() { _cache = HttpRuntime.Cache; } /// <summary> /// 生成sessionid /// </summary> /// <returns></returns> public static string GenerateSid() { long i = 1; byte[] byteArray = Guid.NewGuid().ToByteArray(); foreach (byte b in byteArray) { i *= ((int)b + 1); } return string.Format("{0:x}", i - DateTime.Now.Ticks); } /// <summary> /// 过期时间(单位为秒) /// </summary> /// <value></value> public int Timeout { get { return _timeout; } } /// <summary> /// 获得用户会话状态数据 /// </summary> /// <param name="sid">sid</param> /// <returns>Dictionary<string,object>类型</returns> public Dictionary<string, object> GetSession(string sid) { object session = _cache.Get(sid); if (session != null) return (Dictionary<string, object>)session; Dictionary<string, object> s = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase); _cache.Insert(sid, s, null, DateTime.Now.AddSeconds(_timeout), Cache.NoSlidingExpiration, CacheItemPriority.High, null); return s; } /// <summary> /// 移除用户会话状态数据 /// </summary> /// <param name="sid">sid</param> public void RemoverSession(string sid) { _cache.Remove(sid); } /// <summary> /// 获得用户会话状态数据的数据项的值 /// </summary> /// <param name="sid">sid</param> /// <param name="key">键</param> /// <returns>当前键值不存在时返回null</returns> public object GetValue(string sid, string key) { object session = _cache.Get(sid); if (session == null) { return null; } else { Dictionary<string, object> s = (Dictionary<string, object>)session; object value = null; s.TryGetValue(key, out value); return value; } } /// <summary> /// 设置用户会话状态数据的数据项 /// </summary> /// <param name="sid">sid</param> /// <param name="key">键</param> /// <param name="value">值</param> public void SetItem(string sid, string key, object value) { Dictionary<string, object> session = GetSession(sid); if (session.ContainsKey(key)) session[key] = value; else session.Add(key, value); } /// <summary> /// 移除用户会话状态数据的数据项 /// </summary> /// <param name="sid">sid</param> /// <param name="key">键</param> public void RemoveItem(string sid, string key) { object session = _cache.Get(sid); if (session != null) { Dictionary<string, object> s = (Dictionary<string, object>)session; s.Remove(key); } } } /// <summary> /// session键类(根据实际情况定义) /// </summary> public partial class SessionKey { /// <summary> /// 图片验证码 /// </summary> public const string VERIFYIMAGEVALUE = "verifyImageValue"; /// <summary> /// 安全中心手机验证值 /// </summary> public const string SAFEVERIFYMOBILEVALUE = "safeVerifyMoibleValue"; /// <summary> /// 安全中心更新手机值 /// </summary> public const string UPDATEMOBILEVALUE = "updateMoibleValue"; /// <summary> /// 安全中心更新手机 /// </summary> public const string UPDATEMOBILE = "updateMoible"; /// <summary> /// 安全中心验证密码动作 /// </summary> public const string VALIDATEPASSWORD = "validatePassword"; /// <summary> /// 安全中心验证手机动作 /// </summary> public const string VALIDATEMOBILE = "validateMobile"; /// <summary> /// 安全中心验证邮箱动作 /// </summary> public const string VALIDATEEMAIL = "validateEmail"; }