/// <summary> /// To be called in Page_Load(): /// Load the M object from the storage, propagate it to all sub-controls /// and recursively hide them all below the main control. /// Also sets a global reference to the main control in IIE for testing. /// </summary> /// <typeparam name="M"></typeparam> /// <param name="controlStorage"></param> public static void LoadMain <M>(this IStorageControl <M> controlStorage) where M : class, new() { controlStorage.ClearIfRequested(); Guid sessionOverride; if (!controlStorage.IsPostBack && Guid.TryParse(controlStorage.Request.QueryString["session"], out sessionOverride)) { controlStorage.Main = Main.LoadMain <M>(sessionOverride); } else { switch (controlStorage.GetStorage()) { case Storage.ViewState: controlStorage.Main = (M)controlStorage.ViewState[controlStorage.StorageID()]; break; case Storage.Session: controlStorage.Main = (M)controlStorage.Session[controlStorage.StorageID()]; break; case Storage.Database: var cookie = controlStorage.Request.Cookies[controlStorage.StorageID()]; if (cookie != null) { Guid session; if (Guid.TryParse(cookie["session"], out session)) { Func <byte[], byte[]> filter = null; if (controlStorage.GetEncryptDatabaseStorage()) { filter = x => Crypt.Decrypt(controlStorage.GetSecret(), x); // closure } controlStorage.Main = Main.LoadMain <M>(session, filter); } } break; } } if (controlStorage.Main == null) { controlStorage.Main = new M(); } CurrentMain = controlStorage.Main; ControlRootExtension.SetRoot(controlStorage); controlStorage.PropagateMain(controlStorage.Main); }
/// <summary> /// To be called at the end of OnPreRender(): /// Persist the in this page life-cycle stage immutable Main object. /// </summary> /// <typeparam name="M"></typeparam> /// <param name="controlStorage"></param> public static void SaveMain <M>(this IStorageControl <M> controlStorage) where M : class, new() { var storage = controlStorage.GetStorage(); Trace.Assert(controlStorage.Main != null, "SaveMain() without preceding LoadMain()"); switch (storage) { case Storage.ViewState: controlStorage.ViewState[controlStorage.StorageID()] = controlStorage.Main; break; case Storage.Session: controlStorage.Session[controlStorage.StorageID()] = controlStorage.Main; break; case Storage.Database: Guid session = Guid.NewGuid(); // cannot exist in the database -> will get a new one on SaveMain() var cookie = controlStorage.Request.Cookies[controlStorage.StorageID()]; if (cookie != null) { Guid.TryParse(controlStorage.Request.Cookies[controlStorage.StorageID()]["session"], out session); } Func <byte[], byte[]> filter = null; if (controlStorage.GetEncryptDatabaseStorage()) { filter = x => Crypt.Encrypt(controlStorage.GetSecret(), x); // closure } session = Main.SaveMain(controlStorage.Main, session, filter); var configDays = ConfigurationManager.AppSettings["DatabaseStorageExpires"]; var days = String.IsNullOrWhiteSpace(configDays) ? 1 : int.Parse(configDays); controlStorage.Response.Cookies[controlStorage.StorageID()]["session"] = session.ToString(); controlStorage.Response.Cookies[controlStorage.StorageID()].Expires = DateTime.Now.AddDays(days); break; } }
/// <summary> /// Hook to clear the storage for that control with ?clear=true /// ViewState is reset anyway on GET requests, therefore NOP in that casefa /// GET-arguments: /// clear=[true|false] triggers clearing the storage /// endresponse=[true|false] whether the page at the given URL /// storage=[Session|Database] clears the selected storage type regardless of config /// </summary> /// <typeparam name="M"></typeparam> /// <param name="controlStorage"></param> /// <returns></returns> internal static void ClearIfRequested <M>(this IStorageControl <M> controlStorage) where M : new() { if (!controlStorage.IsPostBack) { bool clear = false; bool.TryParse(controlStorage.Request.QueryString["clear"], out clear); if (clear) { Storage storage; Enum.TryParse <Storage>(controlStorage.Request.QueryString["storage"], true, out storage); if (storage == Storage.ViewState) // no meaningful override given { storage = controlStorage.GetStorage(); } bool endresponse = false; bool.TryParse(controlStorage.Request.QueryString["endresponse"], out endresponse); switch (storage) { case Storage.ViewState: break; case Storage.Session: controlStorage.Session.Remove(controlStorage.StorageID()); break; case Storage.Database: // delete from the database and expire the cookie var cookie = controlStorage.Request.Cookies[controlStorage.StorageID()]; if (cookie != null) { Guid session; Guid.TryParse(controlStorage.Request.Cookies[controlStorage.StorageID()]["session"], out session); using (var db = new ASP_DBEntities()) { var sql = @" DELETE FROM Main WHERE session = @session "; var param = new SqlParameter("session", session); db.Database.ExecuteSqlCommand(sql, param); } controlStorage.Request.Cookies[controlStorage.StorageID()].Expires = DateTime.Now.AddDays(-1); } break; default: throw new NotImplementedException(String.Format( "Storage {0}", storage)); } if (endresponse) { controlStorage.Response.Clear(); controlStorage.Response.End(); } } } }