SessionHelper.cs(20170223)

复制代码
using System;
using System.Web;

namespace System.CRM.Common
{
    #region Session操作助手类-SessionHelper

    /// <summary>
    /// Session操作助手类
    /// </summary>
    public class SessionHelper
    {
        static SessionHelper() { HttpContext.Current.Session.Timeout = 30; }

        /// <summary>
        /// 设置一个Session
        /// </summary>
        /// <param name="key">Session的Key值</param>
        /// <param name="value">Session的Value值</param>
        public static void SetSession(string key, object value)
        {
            HttpContext.Current.Session.Remove(key);
            HttpContext.Current.Session.Add(key, value);
        }

        /// <summary>
        /// 移除Session
        /// </summary>
        /// <param name="key">Session的Key值</param>
        public static void RemoveSession(string key)
        {
            HttpContext.Current.Session.Remove(key);
        }

        /// <summary>
        /// 移除所有Session
        /// </summary>
        public static void RemoveAllSession()
        {
            HttpContext.Current.Session.RemoveAll();
        }

        /// <summary>
        /// 获取Session值
        /// </summary>
        /// <param name="key">Session的Key值</param>
        /// <returns>Session的Value值</returns>
        public static object GetSesstion(string key)
        {
            return HttpContext.Current.Session[key];
        }

        /// <summary>
        /// 获取Session(泛型)
        /// </summary>
        /// <typeparam name="T">希望获得的类型</typeparam>
        /// <param name="key">Session的Key值</param>
        /// <returns>Session的Value值</returns>
        public static T GetSesstion<T>(string key)
        {
            return ((T)(HttpContext.Current.Session[key]));
        }

        /// <summary>
        /// 设置Session超时时间
        /// </summary>
        /// <param name="timeout">超时时间(单位:分)</param>
        public static void SetTimeout(int timeout)
        {
            HttpContext.Current.Session.Timeout = timeout;
        }


        /// <summary>
        /// 获取session超时时间
        /// </summary>
        /// <returns></returns>
        public static int GetTimeout()
        {
            return HttpContext.Current.Session.Timeout;
        }
    }

    #endregion
}
复制代码

 

posted @   妖狐鬼魅  阅读(1744)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
· 零经验选手,Compose 一天开发一款小游戏!
点击右上角即可分享
微信分享提示