• ASP.NET 中的Session统一管理


    在我的实际工作中,ASP.NET中的Session的定义和取消有时是分散的,工作组中的每个人定义Session的时候不一样,并且名称有随意性,所以做了一个Session的统一管理,便于Session的规范化。

    代码如下:

    1. 定义接口:只需要实现 ToString()即可。

    //Interface for Session
        public interface ISession {
            string ToString();
        }

    2. Session 类

        // ManagerInfo 是Model中的一个类,直接继承
       // 将对象放入Session中时,该对象必须是可序列化的
        [Serializable]
        public class LoginSession : ManagerInfo , ISession
        {
            public LoginSession(): base()
            {
                SessionID = "";
            }
    
            public string SessionID { get; set; }
    
            public override int GetHashCode()
            {
                return SessionID.GetHashCode();
            }
    
            public override string ToString()
            {
                if (!string.IsNullOrEmpty(SessionID))
                    return SessionID;
                else
                    return "";
            }
        }
    

    3. Session赋值

    LoginSession currentManager = new LoginSession();
    currentManager.username="Admin";
    currentManager.permission="all";
    currentManager.SessionID = HttpContext.Current.Session.SessionID;
    HttpContext.Current.Session[AppSetting.GlobalSessionName] = currentManager; HttpContext.Current.Session.Timeout = 200;

    4. 取得Session的值

            public static T GetGlobalSessionValue<T>(string _propertyName)
            {
                return GetSessionValue<T>(Common.Const.AppSetting.GlobalSessionName, _propertyName);
            }
    
            public static T GetSessionValue<T>(string _sessionName , string _propertyName)
            {
                T retVal = default(T);
                string propertyName = _propertyName.ToLower();
    
                if (Convert.ToString(HttpContext.Current.Session[_sessionName]) != "")
                {
                    object SessionObject = (object)HttpContext.Current.Session[_sessionName];
    
                    if (SessionObject is ISession)
                    {
                        PropertyInfo[] propertyInfos = SessionObject.GetType().GetProperties();
    
                        foreach (var pi in propertyInfos)
                        {
                            string refName = pi.Name.ToLower();
                            if (propertyName == refName)
                            {
                                retVal = (T)pi.GetValue(SessionObject, null);
                                break;
                            }
                        }                   
                    }
                }
    
                return retVal;
            }
    

    5. 在程序中可以这样取得Session中某一项的值:

    string _tmpstr = Utilities.GetGlobalSessionValue<string>("username");
    int _tmpint = Utilities.GetGlobalSessionValue<int>("pagesize");
    Model.Manager = Utilities.GetGlobalSessionValue<Manager>("ManagerDetail");
    
  • 相关阅读:
    向量空间模型 词袋模型
    python 小点
    python list的append()和extend()方法
    numpy 数组运算
    内置函数和numpy中的min(),max()函数
    python index()函数
    Python支持的正则表达式元字符和语法
    Python多线程
    查看linux机器配置&内核版本
    Shell获取文件的文件名和扩展名的例子
  • 原文地址:https://www.cnblogs.com/jdxx/p/2036469.html
Copyright © 2020-2023  润新知