c# Session写入读取操作

        /// <summary>
        /// 写Session
        /// </summary>
        /// <typeparam name="T">Session键值的类型</typeparam>
        /// <param name="key">Session的键名</param>
        /// <param name="value">Session的键值</param>
        public static void WriteSession<T>(string key, T value)
        {
            )
                return;
            HttpContext.Current.Session[key] = value;
        }

        /// <summary>
        /// 写Session
        /// </summary>
        /// <param name="key">Session的键名</param>
        /// <param name="value">Session的键值</param>
        public static void WriteSession(string key, string value)
        {
            WriteSession<string>(key, value);
        }

        /// <summary>
        /// 读取Session的值
        /// </summary>
        /// <param name="key">Session的键名</param>
        public static string GetSession(string key)
        {
            )
                return string.Empty;
            return HttpContext.Current.Session[key] as string;
        }

        /// <summary>
        /// 读取Session的值
        /// </summary>
        /// <param name="key">Session的键名</param>
        public static T GetSession<T>(string key)
        {
            )
                return default(T);
            return (T)HttpContext.Current.Session[key];
        }

        /// <summary>
        /// 删除指定Session
        /// </summary>
        /// <param name="key">Session的键名</param>
        public static void RemoveSession(string key)
        {
            )
                return;
            HttpContext.Current.Session.Contents.Remove(key);
        }
Session一般用户登录存取用户信息 不过要防止伪造一般 会在用户登录时加入 redis  每次进页面去验证redis 当前用户登录储存的key 是否过期 过期就重写登录 或者没有KEY 就是 仿照Session 登录
上一篇:Oracle与MySQL的几点区别


下一篇:转:靠谱的代码和DRY