ASP.NET 中的Session統一管理

來源:互聯網
上載者:User

在實際工作中,ASP.NET中的Session的定義和取消有時是分散的,工作群組中的每個人定義Session的時候不一樣,並且名稱有隨意性,所以做了一個Session的統一管理,便於Session的正常化。

代碼如下:

1. 定義介面:只需要實現 ToString()即可。

1 //Interface for Session
2     public interface ISession {
3         string ToString();
4     }

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賦值

1 LoginSession currentManager = new LoginSession();
2 currentManager.username="Admin";
3 currentManager.permission="all";
4 currentManager.SessionID
=
HttpContext.Current.Session.SessionID;<br>HttpContext.Current.Session[AppSetting.GlobalSessionName]
= currentManager;
5 HttpContext.Current.Session.Timeout = 200;

4. 取得Session的值

01 public static T GetGlobalSessionValue<T>(string _propertyName)
02 {
03     return GetSessionValue<T>(Common.Const.AppSetting.GlobalSessionName, _propertyName);
04 }
05  
06 public static T GetSessionValue<T>(string _sessionName , string _propertyName)
07 {
08     T retVal = default(T);
09     string propertyName = _propertyName.ToLower();
10  
11     if (Convert.ToString(HttpContext.Current.Session[_sessionName]) != "")
12     {
13         object SessionObject = (object)HttpContext.Current.Session[_sessionName];
14  
15         if (SessionObject is ISession)
16         {
17             PropertyInfo[] propertyInfos = SessionObject.GetType().GetProperties();
18  
19             foreach (var pi in propertyInfos)
20             {
21                 string refName = pi.Name.ToLower();
22                 if (propertyName == refName)
23                 {
24                     retVal = (T)pi.GetValue(SessionObject, null);
25                     break;
26                 }
27             }                  
28         }
29     }
30  
31     return retVal;
32 }

5. 在程式中可以這樣取得Session中某一項的值:

string _tmpstr = Utilities.GetGlobalSessionValue<string>("username");
int _tmpint = Utilities.GetGlobalSessionValue<int>("pagesize");

Model.Manager = Utilities.GetGlobalSessionValue<Manager>("ManagerDetail");

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.