zoukankan      html  css  js  c++  java
  • 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");
    
  • 相关阅读:
    Java正则表达式的总结
    Mysql数据库操作语句总结
    Java 如何抛出异常、自定义异常
    Java获取系统时间的四种方法
    Mysql与Oracle区别
    Oracle与MySQL的SQL语句区别
    sql语句的各种模糊查询
    基于SpringCloud的Microservices架构实战案例
    一个人走的快,一群人才走的远
    那些会阻碍程序员成长的细节[2]
  • 原文地址:https://www.cnblogs.com/jdxx/p/2036469.html
Copyright © 2011-2022 走看看