[ASP.NET]覆蓋某個Session值的方式(II)

在前一篇(覆蓋某個Session值的方式)是在 Page 的 OnPreInit 時,讀取QueryString然後來將某個Session中的值覆蓋過去!
本篇則介紹在BasePage中,Shadow 掉原有的 Session 屬性,然後在自定的 Session 物件中的取得 Session 值的屬性(this[string name])中去把 QueryString 的值來覆蓋

在前一篇(覆蓋某個Session值的方式)是在 Page 的 OnPreInit 時,讀取QueryString然後來將某個Session中的值覆蓋過去!

另外也可以參考「HACK: debug modifications to the ASP.NET session」這篇的方式,自已新增一個Session物件去包原生的Session,然後在BasePage中,Shadow 掉原有的 Session 屬性,然後在自定的 Session 物件中的取得 Session 值的屬性(this[string name])中去把 QueryString 的值來覆蓋,如下,

public class BaseWebPage : Page
{
	//Shadow掉原有的Session屬性
	public IHttpSessionState Session
	{
		get
		{
			return new APSessionState(HttpContext.Current.Session);  
		}
	}
}

public class APSessionState : IHttpSessionState
{
	private readonly string caseKey = "caseNo";
	HttpSessionState _realSession;
	public APSessionState(HttpSessionState realSession)
	{
		_realSession = realSession;
	}

	#region IHttpSessionState Members

	public void Abandon()
	{
		_realSession.Abandon();
	}

	public void Add(string name, object value)
	{
		_realSession.Add(name, value);
	}

	public void Clear()
	{
		_realSession.Clear();
	}

	public int CodePage
	{
		get
		{
			return _realSession.CodePage;
		}
		set
		{
			_realSession.CodePage = value;
		}
	}

	public HttpCookieMode CookieMode
	{
		get
		{
			return _realSession.CookieMode;
		}
	}

	public void CopyTo(Array array, int index)
	{
		_realSession.CopyTo(array, index);
	}

	public int Count
	{
		get
		{
			return _realSession.Count;
		}
	}

	public System.Collections.IEnumerator GetEnumerator()
	{
		return _realSession.GetEnumerator();
	}

	public bool IsCookieless
	{
		get
		{
			return _realSession.IsCookieless;
		}
	}

	public bool IsNewSession
	{
		get
		{
			return _realSession.IsNewSession;
		}
	}

	public bool IsReadOnly
	{
		get
		{
			return _realSession.IsReadOnly;
		}
	}

	public bool IsSynchronized
	{
		get
		{
			return _realSession.IsSynchronized;
		}
	}

	public System.Collections.Specialized.NameObjectCollectionBase.KeysCollection Keys
	{
		get
		{
			return _realSession.Keys;
		}
	}

	public int LCID
	{
		get
		{
			return _realSession.LCID;
		}
		set
		{
			_realSession.LCID = value;
		}
	}

	public SessionStateMode Mode
	{
		get
		{
			return _realSession.Mode;
		}
	}

	public void Remove(string name)
	{
		_realSession.Remove(name);
	}

	public void RemoveAll()
	{
		_realSession.RemoveAll();
	}

	public void RemoveAt(int index)
	{
		_realSession.RemoveAt(index);
	}

	public string SessionID
	{
		get
		{
			return _realSession.SessionID;
		}
	}

	public HttpStaticObjectsCollection StaticObjects
	{
		get
		{
			return _realSession.StaticObjects;
		}
	}

	public object SyncRoot
	{
		get
		{
			return _realSession.SyncRoot;
		}
	}

	public int Timeout
	{
		get
		{
			return _realSession.Timeout;
		}
		set
		{
			_realSession.Timeout = value;
		}
	}

	public object this[int index]
	{
		get
		{
			return _realSession[index];
		}
		set
		{
			_realSession[index] = value;
		}
	}

	public object this[string name]
	{
		get
		{
			if (caseKey.Equals(name))
			{
				var queryCaseNo = HttpContext.Current.Request.QueryString[caseKey];
				var sessionCaseNo = (string)_realSession[caseKey];
				if (!string.IsNullOrWhiteSpace(queryCaseNo) && !string.IsNullOrWhiteSpace(sessionCaseNo))
				{
					if (sessionCaseNo != queryCaseNo)
					{
						_realSession[caseKey] = queryCaseNo;
						// reload other info
					}
				}
			}
			
			return _realSession[name];
		}
		set
		{
			_realSession[name] = value;
		}
	}

	#endregion
}

 

但這種方式如果程式中不使用 Session屬性,而直接使用 HttpContext.Current.Session 就控制不到了!

所以一個系統之中,Coding standards, guidelines, conventions 是很重要的哦!

Hi, 

亂馬客Blog已移到了 「亂馬客​ : Re:從零開始的軟體開發生活

請大家繼續支持 ^_^