using System; using System.Reflection; using System.Web; using System.Web.UI; namespace umbraco.BusinessLogic { /// /// The StateHelper class provides general helper methods for handling sessions, context, viewstate and cookies. /// public class StateHelper { #region Session Helpers /// /// Gets the session value. /// /// /// The key. /// public static T GetSessionValue(string key) { return GetSessionValue(HttpContext.Current, key); } /// /// Gets the session value. /// /// /// The context. /// The key. /// public static T GetSessionValue(HttpContext context, string key) { if (context == null) return default(T); object o = context.Session[key]; if (o == null) return default(T); return (T)o; } /// /// Sets a session value. /// /// The key. /// The value. public static void SetSessionValue(string key, object value) { SetSessionValue(HttpContext.Current, key, value); } /// /// Sets the session value. /// /// The context. /// The key. /// The value. public static void SetSessionValue(HttpContext context, string key, object value) { if (context == null) return; context.Session[key] = value; } #endregion #region Context Helpers /// /// Gets the context value. /// /// /// The key. /// public static T GetContextValue(string key) { return GetContextValue(HttpContext.Current, key); } /// /// Gets the context value. /// /// /// The context. /// The key. /// public static T GetContextValue(HttpContext context, string key) { if (context == null) return default(T); object o = context.Items[key]; if (o == null) return default(T); return (T)o; } /// /// Sets the context value. /// /// The key. /// The value. public static void SetContextValue(string key, object value) { SetContextValue(HttpContext.Current, key, value); } /// /// Sets the context value. /// /// The context. /// The key. /// The value. public static void SetContextValue(HttpContext context, string key, object value) { if (context == null) return; context.Items[key] = value; } #endregion #region ViewState Helpers /// /// Gets the state bag. /// /// private static StateBag GetStateBag() { if (HttpContext.Current == null) return null; Page page = HttpContext.Current.CurrentHandler as Page; if (page == null) return null; Type pageType = typeof(Page); PropertyInfo viewState = pageType.GetProperty("ViewState", BindingFlags.GetProperty | BindingFlags.Instance); if (viewState == null) return null; return viewState.GetValue(page, null) as StateBag; } /// /// Gets the view state value. /// /// /// The key. /// public static T GetViewStateValue(string key) { return GetViewStateValue(GetStateBag(), key); } /// /// Gets a view-state value. /// /// /// The bag. /// The key. /// public static T GetViewStateValue(StateBag bag, string key) { if (bag == null) return default(T); object o = bag[key]; if (o == null) return default(T); return (T)o; } /// /// Sets the view state value. /// /// The key. /// The value. public static void SetViewStateValue(string key, object value) { SetViewStateValue(GetStateBag(), key, value); } /// /// Sets the view state value. /// /// The bag. /// The key. /// The value. public static void SetViewStateValue(StateBag bag, string key, object value) { if (bag != null) bag[key] = value; } #endregion #region Cookie Helpers /// /// Determines whether a cookie has a value with a specified key. /// /// The key. /// /// true if the cookie has a value with the specified key; otherwise, false. /// public static bool HasCookieValue(string key) { return !string.IsNullOrEmpty(GetCookieValue(HttpContext.Current, key)); } /// /// Gets the cookie value. /// /// The key. /// public static string GetCookieValue(string key) { return GetCookieValue(HttpContext.Current, key); } /// /// Gets the cookie value. /// /// The context. /// The key. /// public static string GetCookieValue(HttpContext context, string key) { // Updated by NH to check against session values as well, which is an optional switch used by members string tempValue = null; if (context == null || context.Request == null) return null; HttpCookie cookie = context.Request.Cookies[key]; if (cookie == null) { // Check for session if (context.Session != null && context.Session[key] != null) if (context.Session[key].ToString() != "0") tempValue = context.Session[key].ToString(); } else tempValue = cookie.Value; return tempValue; } /// /// Sets the cookie value. /// /// The key. /// The value. public static void SetCookieValue(string key, string value) { SetCookieValue(HttpContext.Current, key, value); } public static void ClearCookie(string key) { HttpContext ctx = HttpContext.Current; if (ctx.Request.Cookies[key] != null) ctx.Response.Cookies[key].Expires = DateTime.Now; } /// /// Sets the cookie value. /// /// The context. /// The key. /// The value. public static void SetCookieValue(HttpContext context, string key, string value) { if (context == null || context.Request == null) return; HttpCookie cookie = context.Request.Cookies[key]; if (cookie == null) cookie = new HttpCookie(key); cookie.Value = value; // add default exp on a month cookie.Expires = DateTime.Now.AddMonths(1); // if cookie exists, remove context.Response.Cookies.Add(cookie); } #endregion } }