From 2cc709c4609d2509f26edfa34393ec02635e29ed Mon Sep 17 00:00:00 2001 From: Shannon Date: Wed, 16 Mar 2016 14:59:21 +0100 Subject: [PATCH] Removes StateHelper!! Adds a few cookie extensions and http extensions to more easily replace. --- src/Umbraco.Core/HttpContextExtensions.cs | 14 + .../PublishedContentCacheTests.cs | 4 +- .../TestHelpers/BaseDatabaseFactoryTest.cs | 6 +- .../Web/Mvc/UmbracoViewPageTests.cs | 6 +- src/Umbraco.Web/HttpCookieExtensions.cs | 82 +++ src/Umbraco.Web/Install/InstallHelper.cs | 12 +- .../PublishedContentCache.cs | 5 +- .../umbraco.presentation/helper.cs | 7 +- .../umbraco.presentation/library.cs | 27 +- src/Umbraco.Web/umbraco.presentation/macro.cs | 16 +- .../developer/Xslt/xsltVisualize.aspx.cs | 17 +- .../umbraco/preview/PreviewContent.cs | 10 +- .../webservices/CheckForUpgrade.asmx.cs | 9 +- src/umbraco.businesslogic/StateHelper.cs | 521 ------------------ .../umbraco.businesslogic.csproj | 1 - .../businesslogic/member/Member.cs | 51 +- 16 files changed, 147 insertions(+), 641 deletions(-) delete mode 100644 src/umbraco.businesslogic/StateHelper.cs diff --git a/src/Umbraco.Core/HttpContextExtensions.cs b/src/Umbraco.Core/HttpContextExtensions.cs index b4e420dc42..d734d09158 100644 --- a/src/Umbraco.Core/HttpContextExtensions.cs +++ b/src/Umbraco.Core/HttpContextExtensions.cs @@ -4,6 +4,20 @@ namespace Umbraco.Core { public static class HttpContextExtensions { + public static T GetContextItem(this HttpContextBase httpContext, string key) + { + if (httpContext == null) return default(T); + if (httpContext.Items[key] == null) return default(T); + var val = httpContext.Items[key].TryConvertTo(); + if (val) return val.Result; + return default(T); + } + + public static T GetContextItem(this HttpContext httpContext, string key) + { + return new HttpContextWrapper(httpContext).GetContextItem(key); + } + public static string GetCurrentRequestIpAddress(this HttpContextBase httpContext) { if (httpContext == null) diff --git a/src/Umbraco.Tests/Cache/PublishedCache/PublishedContentCacheTests.cs b/src/Umbraco.Tests/Cache/PublishedCache/PublishedContentCacheTests.cs index 52e4d99136..26fadd5d7c 100644 --- a/src/Umbraco.Tests/Cache/PublishedCache/PublishedContentCacheTests.cs +++ b/src/Umbraco.Tests/Cache/PublishedCache/PublishedContentCacheTests.cs @@ -51,9 +51,7 @@ namespace Umbraco.Tests.Cache.PublishedCache base.Initialize(); _httpContextFactory = new FakeHttpContextFactory("~/Home"); - //ensure the StateHelper is using our custom context - StateHelper.HttpContext = _httpContextFactory.HttpContext; - + var settings = SettingsForTests.GenerateMockSettings(); SettingsForTests.ConfigureSettings(settings); diff --git a/src/Umbraco.Tests/TestHelpers/BaseDatabaseFactoryTest.cs b/src/Umbraco.Tests/TestHelpers/BaseDatabaseFactoryTest.cs index 92dc963dcf..e0ed618da2 100644 --- a/src/Umbraco.Tests/TestHelpers/BaseDatabaseFactoryTest.cs +++ b/src/Umbraco.Tests/TestHelpers/BaseDatabaseFactoryTest.cs @@ -391,11 +391,7 @@ namespace Umbraco.Tests.TestHelpers var factory = routeData != null ? new FakeHttpContextFactory(url, routeData) : new FakeHttpContextFactory(url); - - - //set the state helper - StateHelper.HttpContext = factory.HttpContext; - + return factory; } diff --git a/src/Umbraco.Tests/Web/Mvc/UmbracoViewPageTests.cs b/src/Umbraco.Tests/Web/Mvc/UmbracoViewPageTests.cs index a6be0e7869..756309871f 100644 --- a/src/Umbraco.Tests/Web/Mvc/UmbracoViewPageTests.cs +++ b/src/Umbraco.Tests/Web/Mvc/UmbracoViewPageTests.cs @@ -453,11 +453,7 @@ namespace Umbraco.Tests.Web.Mvc var factory = routeData != null ? new FakeHttpContextFactory(url, routeData) : new FakeHttpContextFactory(url); - - - //set the state helper - StateHelper.HttpContext = factory.HttpContext; - + return factory; } diff --git a/src/Umbraco.Web/HttpCookieExtensions.cs b/src/Umbraco.Web/HttpCookieExtensions.cs index 7aec1466da..41cbcf5149 100644 --- a/src/Umbraco.Web/HttpCookieExtensions.cs +++ b/src/Umbraco.Web/HttpCookieExtensions.cs @@ -40,6 +40,16 @@ namespace Umbraco.Web } } + /// + /// Removes the cookie from the request and the response if it exists + /// + /// + /// + public static void ExpireCookie(this HttpContext http, string cookieName) + { + new HttpContextWrapper(http).ExpireCookie(cookieName); + } + public static string GetPreviewCookieValue(this HttpRequestMessage request) { var cookie = request.Headers.GetCookies(Constants.Web.PreviewCookieName).FirstOrDefault(); @@ -53,6 +63,16 @@ namespace Umbraco.Web return null; } + public static string GetPreviewCookieValue(this HttpRequestBase request) + { + return request.GetCookieValue(Constants.Web.PreviewCookieName); + } + + public static string GetPreviewCookieValue(this HttpRequest request) + { + return new HttpRequestWrapper(request).GetPreviewCookieValue(); + } + /// /// Does a preview cookie exist ? /// @@ -63,6 +83,16 @@ namespace Umbraco.Web return request.Cookies[Constants.Web.PreviewCookieName] != null; } + /// + /// Does a preview cookie exist ? + /// + /// + /// + public static bool HasPreviewCookie(this HttpRequest request) + { + return new HttpRequestWrapper(request).HasPreviewCookie(); + } + /// /// Does a preview cookie exist ? /// @@ -73,6 +103,36 @@ namespace Umbraco.Web return request.Cookies[Constants.Web.PreviewCookieName] != null; } + /// + /// Returns the cookie's string value + /// + /// + /// + /// + public static string GetCookieValue(this HttpRequestBase request, string cookieName) + { + var cookie = request.Cookies.Get(cookieName); + if (cookie != null) + { + if (cookie.Value.IsNullOrWhiteSpace() == false) + { + return cookie.Value; + } + } + return null; + } + + /// + /// Returns the cookie's string value + /// + /// + /// + /// + public static string GetCookieValue(this HttpRequest request, string cookieName) + { + return new HttpRequestWrapper(request).GetCookieValue(cookieName); + } + /// /// Does a cookie exist with the specified key ? /// @@ -84,6 +144,17 @@ namespace Umbraco.Web return request.Cookies[key] != null; } + /// + /// Does a cookie exist with the specified key ? + /// + /// + /// + /// + public static bool HasCookie(this HttpRequest request, string key) + { + return request.Cookies[key] != null; + } + /// /// Is there a cookie with the key supplied and does it have a value that is not empty /// @@ -96,5 +167,16 @@ namespace Umbraco.Web && request.Cookies[key].Value != null && request.Cookies[key].Value.IsNullOrWhiteSpace() == false; } + + /// + /// Is there a cookie with the key supplied and does it have a value that is not empty + /// + /// + /// + /// + public static bool HasCookieValue(this HttpRequest request, string key) + { + return new HttpRequestWrapper(request).HasCookieValue(key); + } } } \ No newline at end of file diff --git a/src/Umbraco.Web/Install/InstallHelper.cs b/src/Umbraco.Web/Install/InstallHelper.cs index 143067f6a1..ae0606d729 100644 --- a/src/Umbraco.Web/Install/InstallHelper.cs +++ b/src/Umbraco.Web/Install/InstallHelper.cs @@ -16,6 +16,7 @@ using Umbraco.Core.Logging; using Umbraco.Core.Persistence; using Umbraco.Web.Install.InstallSteps; using Umbraco.Web.Install.Models; +using Umbraco.Web; namespace Umbraco.Web.Install @@ -104,18 +105,19 @@ namespace Umbraco.Web.Install // Check for current install Id var installId = Guid.NewGuid(); - var installCookie = new StateHelper.Cookies.Cookie("umb_installId", 1); - if (string.IsNullOrEmpty(installCookie.GetValue()) == false) + + var installCookie = _umbContext.HttpContext.Request.GetPreviewCookieValue(); + if (string.IsNullOrEmpty(installCookie) == false) { - if (Guid.TryParse(installCookie.GetValue(), out installId)) + if (Guid.TryParse(installCookie, out installId)) { // check that it's a valid Guid if (installId == Guid.Empty) installId = Guid.NewGuid(); } } - installCookie.SetValue(installId.ToString()); - + _umbContext.HttpContext.Response.Cookies.Set(new HttpCookie("umb_installId", "1")); + string dbProvider = string.Empty; if (IsBrandNewInstall == false) dbProvider = ApplicationContext.Current.DatabaseContext.DatabaseProvider.ToString(); diff --git a/src/Umbraco.Web/PublishedCache/XmlPublishedCache/PublishedContentCache.cs b/src/Umbraco.Web/PublishedCache/XmlPublishedCache/PublishedContentCache.cs index 715f00a735..182efd54db 100644 --- a/src/Umbraco.Web/PublishedCache/XmlPublishedCache/PublishedContentCache.cs +++ b/src/Umbraco.Web/PublishedCache/XmlPublishedCache/PublishedContentCache.cs @@ -12,8 +12,10 @@ using Umbraco.Core.Models; using Umbraco.Core.Models.PublishedContent; using Umbraco.Core.Xml; using Umbraco.Web.Routing; +using Umbraco.Web; using umbraco; using System.Linq; +using System.Web; using umbraco.BusinessLogic; using umbraco.presentation.preview; @@ -47,7 +49,8 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache if (preview) { var previewContent = PreviewContentCache.GetOrCreateValue(context); // will use the ctor with no parameters - previewContent.EnsureInitialized(context.UmbracoUser, StateHelper.Cookies.Preview.GetValue(), true, () => + var previewVal = HttpContext.Current.Request.GetPreviewCookieValue(); + previewContent.EnsureInitialized(context.UmbracoUser, previewVal, true, () => { if (previewContent.ValidPreviewSet) previewContent.LoadPreviewset(); diff --git a/src/Umbraco.Web/umbraco.presentation/helper.cs b/src/Umbraco.Web/umbraco.presentation/helper.cs index d44b9e3dcf..7e845211b3 100644 --- a/src/Umbraco.Web/umbraco.presentation/helper.cs +++ b/src/Umbraco.Web/umbraco.presentation/helper.cs @@ -10,6 +10,7 @@ using Umbraco.Core.Profiling; using umbraco.BusinessLogic; using System.Xml; using umbraco.presentation; +using Umbraco.Web; using Umbraco.Web.UI.Pages; namespace umbraco @@ -103,9 +104,9 @@ namespace umbraco attributeValue = HttpContext.Current.Request[keyName]; break; case "%": - attributeValue = StateHelper.GetSessionValue(keyName); - if (String.IsNullOrEmpty(attributeValue)) - attributeValue = StateHelper.GetCookieValue(keyName); + attributeValue = HttpContext.Current.Session[keyName] != null ? HttpContext.Current.Session[keyName].ToString() : null; + if (string.IsNullOrEmpty(attributeValue)) + attributeValue = HttpContext.Current.Request.GetCookieValue(keyName); break; case "#": if (pageElements[keyName] != null) diff --git a/src/Umbraco.Web/umbraco.presentation/library.cs b/src/Umbraco.Web/umbraco.presentation/library.cs index 39086ad1c7..dc7ae9da1e 100644 --- a/src/Umbraco.Web/umbraco.presentation/library.cs +++ b/src/Umbraco.Web/umbraco.presentation/library.cs @@ -210,29 +210,8 @@ namespace umbraco xd.LoadXml(string.Format("Could not convert JSON to XML. Error: {0}", ex)); return xd.CreateNavigator().Select("/error"); } - } - - /// - /// Add a session variable to the current user - /// - /// The Key of the variable - /// The Value - public static void setSession(string key, string value) - { - if (HttpContext.Current.Session != null) - HttpContext.Current.Session[key] = value; - } - - /// - /// Add a cookie variable to the current user - /// - /// The Key of the variable - /// The Value of the variable - public static void setCookie(string key, string value) - { - StateHelper.SetCookieValue(key, value); - } - + } + /// /// Returns a string with a friendly url from a node. /// IE.: Instead of having /482 (id) as an url, you can have @@ -1683,7 +1662,7 @@ namespace umbraco public static string RequestCookies(string key) { // zb-00004 #29956 : refactor cookies handling - var value = StateHelper.GetCookieValue(key); + var value = HttpContext.Current.Request.GetCookieValue(key); return value ?? ""; } diff --git a/src/Umbraco.Web/umbraco.presentation/macro.cs b/src/Umbraco.Web/umbraco.presentation/macro.cs index bcfaae4723..859a719c37 100644 --- a/src/Umbraco.Web/umbraco.presentation/macro.cs +++ b/src/Umbraco.Web/umbraco.presentation/macro.cs @@ -265,7 +265,10 @@ namespace umbraco { TraceInfo("renderMacro", macroInfo, excludeProfiling: true); - StateHelper.SetContextValue(MacrosAddedKey, StateHelper.GetContextValue(MacrosAddedKey) + 1); + if (HttpContext.Current != null) + { + HttpContext.Current.Items[MacrosAddedKey] = HttpContext.Current.GetContextItem(MacrosAddedKey) + 1; + } // zb-00037 #29875 : parse attributes here (and before anything else) foreach (MacroPropertyModel prop in Model.Properties) @@ -1379,7 +1382,7 @@ namespace umbraco else oControl.ID = string.Format("{0}_{1}", fileName.Substring(slashIndex, fileName.IndexOf(".ascx") - slashIndex), - StateHelper.GetContextValue(MacrosAddedKey)); + HttpContext.Current.GetContextItem(MacrosAddedKey)); TraceInfo(LoadUserControlKey, string.Format("Usercontrol added with id '{0}'", oControl.ID)); @@ -1533,9 +1536,12 @@ namespace umbraco //TODO: This is the worst thing ever. This will also not work if people decide to put their own // custom auth system in place. - HttpCookie inCookie = StateHelper.Cookies.UserContext.RequestCookie; - var cookie = new Cookie(inCookie.Name, inCookie.Value, inCookie.Path, - HttpContext.Current.Request.ServerVariables["SERVER_NAME"]); + HttpCookie inCookie = HttpContext.Current.Request.Cookies[UmbracoConfig.For.UmbracoSettings().Security.AuthCookieName]; + if (inCookie == null) throw new NullReferenceException("No auth cookie found"); + var cookie = new Cookie(UmbracoConfig.For.UmbracoSettings().Security.AuthCookieName, + inCookie.Value, + inCookie.Path, + HttpContext.Current.Request.ServerVariables["SERVER_NAME"]); myHttpWebRequest.CookieContainer = new CookieContainer(); myHttpWebRequest.CookieContainer.Add(cookie); diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/developer/Xslt/xsltVisualize.aspx.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/developer/Xslt/xsltVisualize.aspx.cs index ec8333e074..255c132187 100644 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/developer/Xslt/xsltVisualize.aspx.cs +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/developer/Xslt/xsltVisualize.aspx.cs @@ -9,17 +9,15 @@ using System.Xml; using System.IO; using Umbraco.Core; using Umbraco.Core.IO; +using Umbraco.Web; using Umbraco.Web.UI.Pages; namespace umbraco.presentation.umbraco.developer.Xslt { [WebformsPageTreeAuthorize(Constants.Trees.Xslt)] public partial class xsltVisualize : UmbracoEnsuredPage - { - - // zb-00004 #29956 : refactor cookies names & handling - static global::umbraco.BusinessLogic.StateHelper.Cookies.Cookie cookie - = new global::umbraco.BusinessLogic.StateHelper.Cookies.Cookie("UMB_XSLTVISPG", TimeSpan.FromMinutes(20)); // was "XSLTVisualizerPage" + { + private const string XsltVisualizeCookieName = "UMB_XSLTVISPG"; protected void Page_Load(object sender, EventArgs e) { @@ -27,8 +25,8 @@ namespace umbraco.presentation.umbraco.developer.Xslt { // Check if cookie exists in the current request. // zb-00004 #29956 : refactor cookies names & handling - if (cookie.HasValue) - contentPicker.Value = cookie.GetValue(); + if (Request.HasCookieValue(XsltVisualizeCookieName)) + contentPicker.Value = Request.GetCookieValue(XsltVisualizeCookieName); } } @@ -91,7 +89,10 @@ namespace umbraco.presentation.umbraco.developer.Xslt // add cookie with current page // zb-00004 #29956 : refactor cookies names & handling - cookie.SetValue(contentPicker.Value); + Response.Cookies.Set(new HttpCookie(XsltVisualizeCookieName, contentPicker.Value) + { + Expires = DateTime.Now + TimeSpan.FromMinutes(20) + }); } } diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/preview/PreviewContent.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/preview/PreviewContent.cs index dbd3c8b806..052882d3f4 100644 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/preview/PreviewContent.cs +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/preview/PreviewContent.cs @@ -216,26 +216,24 @@ namespace umbraco.presentation.preview public void ActivatePreviewCookie() { - // zb-00004 #29956 : refactor cookies names & handling - StateHelper.Cookies.Preview.SetValue(PreviewSet.ToString()); + HttpContext.Current.Response.Cookies.Set(new HttpCookie(Constants.Web.PreviewCookieName, PreviewSet.ToString())); } public static void ClearPreviewCookie() { - // zb-00004 #29956 : refactor cookies names & handling if (UmbracoContext.Current.UmbracoUser != null) { - if (StateHelper.Cookies.Preview.HasValue) + if (HttpContext.Current.Request.HasPreviewCookie()) { DeletePreviewFile( UmbracoContext.Current.UmbracoUser.Id, new FileInfo(GetPreviewsetPath( UmbracoContext.Current.UmbracoUser.Id, - new Guid(StateHelper.Cookies.Preview.GetValue())))); + new Guid(HttpContext.Current.Request.GetPreviewCookieValue())))); } } - StateHelper.Cookies.Preview.Clear(); + HttpContext.Current.ExpireCookie(Constants.Web.PreviewCookieName); } } } diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/webservices/CheckForUpgrade.asmx.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/webservices/CheckForUpgrade.asmx.cs index eb9ce84a6c..1dae965735 100644 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/webservices/CheckForUpgrade.asmx.cs +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/webservices/CheckForUpgrade.asmx.cs @@ -6,6 +6,7 @@ using System.Web.Script.Services; using Umbraco.Core; using Umbraco.Web.WebServices; using Umbraco.Core.Configuration; +using Umbraco.Web; namespace umbraco.presentation.webservices @@ -56,10 +57,10 @@ namespace umbraco.presentation.webservices // Check for current install Id Guid installId = Guid.NewGuid(); - var installCookie = new BusinessLogic.StateHelper.Cookies.Cookie("umb_installId", 1); - if (string.IsNullOrEmpty(installCookie.GetValue()) == false) + var installCookie = Context.Request.GetCookieValue("umb_installId"); + if (string.IsNullOrEmpty(installCookie) == false) { - if (Guid.TryParse(installCookie.GetValue(), out installId)) + if (Guid.TryParse(installCookie, out installId)) { // check that it's a valid Guid if (installId == Guid.Empty) @@ -67,7 +68,7 @@ namespace umbraco.presentation.webservices } } - installCookie.SetValue(installId.ToString()); + Context.Response.Cookies.Set(new HttpCookie("umb_installId", installId.ToString())); string dbProvider = string.Empty; if (string.IsNullOrEmpty(global::Umbraco.Core.Configuration.GlobalSettings.ConfigurationStatus) == false) diff --git a/src/umbraco.businesslogic/StateHelper.cs b/src/umbraco.businesslogic/StateHelper.cs deleted file mode 100644 index 61df4318ab..0000000000 --- a/src/umbraco.businesslogic/StateHelper.cs +++ /dev/null @@ -1,521 +0,0 @@ -using System; -using System.Reflection; -using System.Web; -using System.Web.UI; -using Umbraco.Core; -using Umbraco.Core.Configuration; - -namespace umbraco.BusinessLogic -{ - /// - /// The StateHelper class provides general helper methods for handling sessions, context, viewstate and cookies. - /// - [Obsolete("DO NOT USE THIS ANYMORE! REPLACE ALL CALLS WITH NEW EXTENSION METHODS")] - public class StateHelper - { - - private static HttpContextBase _customHttpContext; - - /// - /// Gets/sets the HttpContext object, this is generally used for unit testing. By default this will - /// use the HttpContext.Current - /// - internal static HttpContextBase HttpContext - { - get - { - if (_customHttpContext == null && System.Web.HttpContext.Current != null) - { - //return the current HttpContxt, do NOT store this in the _customHttpContext field - //as it will persist across reqeusts! - return new HttpContextWrapper(System.Web.HttpContext.Current); - } - - if (_customHttpContext == null && System.Web.HttpContext.Current == null) - { - throw new NullReferenceException("The HttpContext property has not been set or the object execution is not running inside of an HttpContext"); - } - return _customHttpContext; - } - set { _customHttpContext = value; } - } - - #region Session Helpers - - /// - /// Gets the session value. - /// - /// - /// The key. - /// - public static T GetSessionValue(string key) - { - return GetSessionValue(HttpContext, key); - } - - /// - /// Gets the session value. - /// - /// - /// The context. - /// The key. - /// - [Obsolete("Use the GetSessionValue accepting an HttpContextBase instead")] - public static T GetSessionValue(HttpContext context, string key) - { - return GetSessionValue(new HttpContextWrapper(context), key); - } - - /// - /// Gets the session value. - /// - /// - /// The context. - /// The key. - /// - public static T GetSessionValue(HttpContextBase 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, key, value); - } - - /// - /// Sets the session value. - /// - /// The context. - /// The key. - /// The value. - [Obsolete("Use the SetSessionValue accepting an HttpContextBase instead")] - public static void SetSessionValue(HttpContext context, string key, object value) - { - SetSessionValue(new HttpContextWrapper(context), key, value); - } - - /// - /// Sets the session value. - /// - /// The context. - /// The key. - /// The value. - public static void SetSessionValue(HttpContextBase 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, key); - } - - /// - /// Gets the context value. - /// - /// - /// The context. - /// The key. - /// - [Obsolete("Use the GetContextValue accepting an HttpContextBase instead")] - public static T GetContextValue(HttpContext context, string key) - { - return GetContextValue(new HttpContextWrapper(context), key); - } - - /// - /// Gets the context value. - /// - /// - /// The context. - /// The key. - /// - public static T GetContextValue(HttpContextBase 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, key, value); - } - - /// - /// Sets the context value. - /// - /// The context. - /// The key. - /// The value. - [Obsolete("Use the SetContextValue accepting an HttpContextBase instead")] - public static void SetContextValue(HttpContext context, string key, object value) - { - SetContextValue(new HttpContextWrapper(context), key, value); - } - - /// - /// Sets the context value. - /// - /// The context. - /// The key. - /// The value. - public static void SetContextValue(HttpContextBase 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; - - var page = HttpContext.CurrentHandler as Page; - if (page == null) - return null; - - var pageType = typeof(Page); - var 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. - /// - [Obsolete("Use !string.IsNullOrEmpty(GetCookieValue(key))", false)] - public static bool HasCookieValue(string key) - { - return !string.IsNullOrEmpty(GetCookieValue(key)); - } - - - - /// - /// Gets the cookie value. - /// - /// The key. - /// - public static string GetCookieValue(string key) - { - if (!Cookies.HasCookies) - return null; - var cookie = HttpContext.Request.Cookies[key]; - return cookie == null ? null : cookie.Value; - } - - /// - /// Sets the cookie value. - /// - /// The key. - /// The value. - public static void SetCookieValue(string key, string value) - { - SetCookieValue(key, value, 30d); // default Umbraco expires is 30 days - } - - /// - /// Sets the cookie value including the number of days to persist the cookie - /// - /// The key. - /// The value. - /// How long the cookie should be present in the browser - public static void SetCookieValue(string key, string value, double daysToPersist) - { - if (!Cookies.HasCookies) - return; - var context = HttpContext; - - HttpCookie cookie = new HttpCookie(key, value); - cookie.Expires = DateTime.Now.AddDays(daysToPersist); - context.Response.Cookies.Set(cookie); - - cookie = context.Request.Cookies[key]; - if (cookie != null) - cookie.Value = value; - } - - // zb-00004 #29956 : refactor cookies names & handling - public static class Cookies - { - /* - * helper class to manage cookies - * - * beware! SetValue(string value) does _not_ set expires, unless the cookie has been - * configured to have one. This allows us to have cookies w/out an expires timespan. - * However, default behavior in Umbraco was to set expires to 30days by default. This - * must now be managed in the Cookie constructor or by using an overriden SetValue(...). - * - * we currently reproduce this by configuring each cookie with a 30d expires, but does - * that actually make sense? shouldn't some cookie have _no_ expires? - */ - static readonly Cookie _preview = new Cookie(Constants.Web.PreviewCookieName, TimeSpan.Zero); // was "PreviewSet" - static readonly Cookie _userContext = new Cookie(UmbracoConfig.For.UmbracoSettings().Security.AuthCookieName, 30d); // was "UserContext" - static readonly Cookie _member = new Cookie("UMB_MEMBER", 30d); // was "umbracoMember" - - public static Cookie Preview { get { return _preview; } } - public static Cookie UserContext { get { return _userContext; } } - public static Cookie Member { get { return _member; } } - - public static bool HasCookies - { - get - { - var context = HttpContext; - // although just checking context should be enough?! - // but in some (replaced) umbraco code, everything is checked... - return context != null - && context.Request != null & context.Request.Cookies != null - && context.Response != null && context.Response.Cookies != null; - } - } - - public static void ClearAll() - { - HttpContext.Response.Cookies.Clear(); - } - - public class Cookie - { - const string cookiesExtensionConfigKey = "umbracoCookiesExtension"; - - static readonly string _ext; - TimeSpan _expires; - string _key; - - static Cookie() - { - var appSettings = System.Configuration.ConfigurationManager.AppSettings; - _ext = appSettings[cookiesExtensionConfigKey] == null ? "" : "_" + (string)appSettings[cookiesExtensionConfigKey]; - } - - public Cookie(string key) - : this(key, TimeSpan.Zero, true) - { } - - public Cookie(string key, double days) - : this(key, TimeSpan.FromDays(days), true) - { } - - public Cookie(string key, TimeSpan expires) - : this(key, expires, true) - { } - - public Cookie(string key, bool appendExtension) - : this(key, TimeSpan.Zero, appendExtension) - { } - - public Cookie(string key, double days, bool appendExtension) - : this(key, TimeSpan.FromDays(days), appendExtension) - { } - - public Cookie(string key, TimeSpan expires, bool appendExtension) - { - _key = appendExtension ? key + _ext : key; - _expires = expires; - } - - public string Key - { - get { return _key; } - } - - public bool HasValue - { - get { return RequestCookie != null; } - } - - public string GetValue() - { - return RequestCookie == null ? null : RequestCookie.Value; - } - public void SetValue(string value) - { - SetValueWithDate(value, _expires == TimeSpan.Zero ? DateTime.MinValue : DateTime.Now + _expires); - } - - public void SetValue(string value, double days) - { - SetValue(value, DateTime.Now.AddDays(days)); - } - - public void SetValue(string value, TimeSpan expires) - { - SetValue(value, expires == TimeSpan.Zero ? DateTime.MinValue : DateTime.Now + expires); - } - - public void SetValue(string value, DateTime expires) - { - SetValueWithDate(value, expires); - } - - private void SetValueWithDate(string value, DateTime expires) - { - var cookie = new HttpCookie(_key, value); - - if (GlobalSettings.UseSSL) - cookie.Secure = true; - - //ensure http only, this should only be able to be accessed via the server - cookie.HttpOnly = true; - - //set an expiry date if not min value, otherwise leave it as a session cookie. - if (expires != DateTime.MinValue) - { - cookie.Expires = expires; - } - - ResponseCookie = cookie; - - // original Umbraco code also does this - // so we can GetValue() back what we previously set - cookie = RequestCookie; - if (cookie != null) - cookie.Value = value; - } - - public void Clear() - { - if (RequestCookie != null || ResponseCookie != null) - { - var cookie = new HttpCookie(_key); - cookie.Expires = DateTime.Now.AddDays(-1); - ResponseCookie = cookie; - } - } - - public void Remove() - { - // beware! will not clear browser's cookie - // you probably want to use .Clear() - HttpContext.Response.Cookies.Remove(_key); - } - - public HttpCookie RequestCookie - { - get - { - return HttpContext.Request.Cookies[_key]; - } - } - - public HttpCookie ResponseCookie - { - get - { - return HttpContext.Response.Cookies[_key]; - } - set - { - // .Set() ensures the uniqueness of cookies in the cookie collection - // ie it is the same as .Remove() + .Add() -- .Add() allows duplicates - HttpContext.Response.Cookies.Set(value); - } - } - } - } - - #endregion - } -} diff --git a/src/umbraco.businesslogic/umbraco.businesslogic.csproj b/src/umbraco.businesslogic/umbraco.businesslogic.csproj index 6333c853f9..8b3c5b2103 100644 --- a/src/umbraco.businesslogic/umbraco.businesslogic.csproj +++ b/src/umbraco.businesslogic/umbraco.businesslogic.csproj @@ -188,7 +188,6 @@ Code - Code diff --git a/src/umbraco.cms/businesslogic/member/Member.cs b/src/umbraco.cms/businesslogic/member/Member.cs index a6922f580d..f77209ce49 100644 --- a/src/umbraco.cms/businesslogic/member/Member.cs +++ b/src/umbraco.cms/businesslogic/member/Member.cs @@ -713,14 +713,6 @@ namespace umbraco.cms.businesslogic.member return string.Format("{0}{1}", CacheKeys.MemberBusinessLogicCacheKey, id); } - [Obsolete("Only use .NET Membership APIs to handle state now", true)] - static void ClearMemberState() - { - // zb-00004 #29956 : refactor cookies names & handling - StateHelper.Cookies.Member.Clear(); - FormsAuthentication.SignOut(); - } - #endregion #region MemberHandle functions @@ -844,48 +836,7 @@ namespace umbraco.cms.businesslogic.member public static void RemoveMemberFromCache(int NodeId) { ApplicationContext.Current.ApplicationCache.RuntimeCache.ClearCacheItem(GetCacheKey(NodeId)); - } - - /// - /// Deletes the member cookie from the browser - /// - /// Can be used in the public website - /// - /// Member - [Obsolete("Obsolete, use the ClearMemberFromClient(int NodeId) instead", false)] - public static void ClearMemberFromClient(Member m) - { - - if (m != null) - ClearMemberFromClient(m.Id); - else - { - // If the member doesn't exists as an object, we'll just make sure that cookies are cleared - // zb-00035 #29931 : cleanup member state management - ClearMemberState(); - } - - FormsAuthentication.SignOut(); - } - - /// - /// Deletes the member cookie from the browser - /// - /// Can be used in the public website - /// - /// The Node id of the member to clear - [Obsolete("Use FormsAuthentication.SignOut instead")] - public static void ClearMemberFromClient(int NodeId) - { - // zb-00035 #29931 : cleanup member state management - // NH 4.7.1: We'll no longer use legacy Umbraco cookies to handle members - // ClearMemberState(); - - FormsAuthentication.SignOut(); - - RemoveMemberFromCache(NodeId); - - } + } /// /// Retrieve a collection of members in the cache