From dc36fa1290ad7e606146ab532722ae709b979da2 Mon Sep 17 00:00:00 2001 From: Bjarke Berg Date: Thu, 27 Feb 2020 10:43:09 +0100 Subject: [PATCH 1/2] Fix issues in views --- .../Umbraco/PartialViewMacros/Templates/EditProfile.cshtml | 5 +++-- .../Umbraco/PartialViewMacros/Templates/LoginStatus.cshtml | 3 ++- .../PartialViewMacros/Templates/RegisterMember.cshtml | 3 ++- src/Umbraco.Web/Composing/Current.cs | 2 ++ 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Umbraco.Web.UI/Umbraco/PartialViewMacros/Templates/EditProfile.cshtml b/src/Umbraco.Web.UI/Umbraco/PartialViewMacros/Templates/EditProfile.cshtml index 74ec033f25..5f75f8d792 100644 --- a/src/Umbraco.Web.UI/Umbraco/PartialViewMacros/Templates/EditProfile.cshtml +++ b/src/Umbraco.Web.UI/Umbraco/PartialViewMacros/Templates/EditProfile.cshtml @@ -1,11 +1,12 @@ @using System.Web.Mvc.Html @using ClientDependency.Core.Mvc @using Umbraco.Web +@using Umbraco.Web.Composing @using Umbraco.Web.Controllers @inherits Umbraco.Web.Macros.PartialViewMacroPage @{ - var profileModel = Members.GetCurrentMemberProfileModel(); + var profileModel = Current.MembershipHelper.GetCurrentMemberProfileModel(); Html.EnableClientValidation(); Html.EnableUnobtrusiveJavaScript(); @@ -19,7 +20,7 @@ @*NOTE: This RenderJsHere code should be put on your main template page where the rest of your script tags are placed*@ @Html.RenderJsHere() -@if (Members.IsLoggedIn() && profileModel != null) +@if (Current.MembershipHelper.IsLoggedIn() && profileModel != null) { if (success) { diff --git a/src/Umbraco.Web.UI/Umbraco/PartialViewMacros/Templates/LoginStatus.cshtml b/src/Umbraco.Web.UI/Umbraco/PartialViewMacros/Templates/LoginStatus.cshtml index 8eadbb342f..78b06151af 100644 --- a/src/Umbraco.Web.UI/Umbraco/PartialViewMacros/Templates/LoginStatus.cshtml +++ b/src/Umbraco.Web.UI/Umbraco/PartialViewMacros/Templates/LoginStatus.cshtml @@ -1,11 +1,12 @@ @using System.Web.Mvc.Html @using Umbraco.Web +@using Umbraco.Web.Composing @using Umbraco.Web.Models @using Umbraco.Web.Controllers @inherits Umbraco.Web.Macros.PartialViewMacroPage @{ - var loginStatusModel = Members.GetCurrentLoginStatus(); + var loginStatusModel = Current.MembershipHelper.GetCurrentLoginStatus(); var logoutModel = new PostRedirectModel(); @* diff --git a/src/Umbraco.Web.UI/Umbraco/PartialViewMacros/Templates/RegisterMember.cshtml b/src/Umbraco.Web.UI/Umbraco/PartialViewMacros/Templates/RegisterMember.cshtml index 804e2307f0..81389f4a3d 100644 --- a/src/Umbraco.Web.UI/Umbraco/PartialViewMacros/Templates/RegisterMember.cshtml +++ b/src/Umbraco.Web.UI/Umbraco/PartialViewMacros/Templates/RegisterMember.cshtml @@ -1,6 +1,7 @@ @using System.Web.Mvc.Html @using ClientDependency.Core.Mvc @using Umbraco.Web +@using Umbraco.Web.Composing @using Umbraco.Web.Controllers @inherits Umbraco.Web.Macros.PartialViewMacroPage @@ -12,7 +13,7 @@ var registerModel = Members.CreateRegistrationModel("Custom Member"); *@ - var registerModel = Members.CreateRegistrationModel(); + var registerModel = Current.MembershipHelper.CreateRegistrationModel(); @* Configurable here: diff --git a/src/Umbraco.Web/Composing/Current.cs b/src/Umbraco.Web/Composing/Current.cs index 8d4af6703e..081b808cac 100644 --- a/src/Umbraco.Web/Composing/Current.cs +++ b/src/Umbraco.Web/Composing/Current.cs @@ -26,6 +26,7 @@ using Umbraco.Web.HealthCheck; using Umbraco.Web.Mvc; using Umbraco.Web.PublishedCache; using Umbraco.Web.Routing; +using Umbraco.Web.Security; using Umbraco.Web.Services; using Umbraco.Web.Trees; using Umbraco.Web.WebApi; @@ -262,6 +263,7 @@ namespace Umbraco.Web.Composing public static IUmbracoVersion UmbracoVersion => Factory.GetInstance(); public static IPublishedUrlProvider PublishedUrlProvider => Factory.GetInstance(); public static IMenuItemCollectionFactory MenuItemCollectionFactory => Factory.GetInstance(); + public static MembershipHelper MembershipHelper => Factory.GetInstance(); #endregion } From f0dee4506922428170e7f3c43160b3124d87f6ec Mon Sep 17 00:00:00 2001 From: Bjarke Berg Date: Thu, 27 Feb 2020 10:41:43 +0100 Subject: [PATCH 2/2] Refactored UmbracoApplication.Restart --- .../Net/IUmbracoApplicationLifetime.cs | 1 + .../Templates/EditProfile.cshtml | 5 ++- .../Templates/LoginStatus.cshtml | 3 +- .../Templates/RegisterMember.cshtml | 3 +- .../AspNetUmbracoApplicationLifetime.cs | 17 +++++++- src/Umbraco.Web/Composing/Current.cs | 3 ++ .../Editors/PackageInstallController.cs | 16 ++++---- src/Umbraco.Web/Mvc/ContentModelBinder.cs | 8 +++- src/Umbraco.Web/Runtime/WebInitialComposer.cs | 2 +- src/Umbraco.Web/UmbracoApplication.cs | 41 ------------------- 10 files changed, 43 insertions(+), 56 deletions(-) diff --git a/src/Umbraco.Core/Net/IUmbracoApplicationLifetime.cs b/src/Umbraco.Core/Net/IUmbracoApplicationLifetime.cs index 4e9f655414..9723d116bd 100644 --- a/src/Umbraco.Core/Net/IUmbracoApplicationLifetime.cs +++ b/src/Umbraco.Core/Net/IUmbracoApplicationLifetime.cs @@ -2,6 +2,7 @@ namespace Umbraco.Net { public interface IUmbracoApplicationLifetime { + bool IsRestarting { get; } void Restart(); } } diff --git a/src/Umbraco.Web.UI/Umbraco/PartialViewMacros/Templates/EditProfile.cshtml b/src/Umbraco.Web.UI/Umbraco/PartialViewMacros/Templates/EditProfile.cshtml index 74ec033f25..5f75f8d792 100644 --- a/src/Umbraco.Web.UI/Umbraco/PartialViewMacros/Templates/EditProfile.cshtml +++ b/src/Umbraco.Web.UI/Umbraco/PartialViewMacros/Templates/EditProfile.cshtml @@ -1,11 +1,12 @@ @using System.Web.Mvc.Html @using ClientDependency.Core.Mvc @using Umbraco.Web +@using Umbraco.Web.Composing @using Umbraco.Web.Controllers @inherits Umbraco.Web.Macros.PartialViewMacroPage @{ - var profileModel = Members.GetCurrentMemberProfileModel(); + var profileModel = Current.MembershipHelper.GetCurrentMemberProfileModel(); Html.EnableClientValidation(); Html.EnableUnobtrusiveJavaScript(); @@ -19,7 +20,7 @@ @*NOTE: This RenderJsHere code should be put on your main template page where the rest of your script tags are placed*@ @Html.RenderJsHere() -@if (Members.IsLoggedIn() && profileModel != null) +@if (Current.MembershipHelper.IsLoggedIn() && profileModel != null) { if (success) { diff --git a/src/Umbraco.Web.UI/Umbraco/PartialViewMacros/Templates/LoginStatus.cshtml b/src/Umbraco.Web.UI/Umbraco/PartialViewMacros/Templates/LoginStatus.cshtml index 8eadbb342f..78b06151af 100644 --- a/src/Umbraco.Web.UI/Umbraco/PartialViewMacros/Templates/LoginStatus.cshtml +++ b/src/Umbraco.Web.UI/Umbraco/PartialViewMacros/Templates/LoginStatus.cshtml @@ -1,11 +1,12 @@ @using System.Web.Mvc.Html @using Umbraco.Web +@using Umbraco.Web.Composing @using Umbraco.Web.Models @using Umbraco.Web.Controllers @inherits Umbraco.Web.Macros.PartialViewMacroPage @{ - var loginStatusModel = Members.GetCurrentLoginStatus(); + var loginStatusModel = Current.MembershipHelper.GetCurrentLoginStatus(); var logoutModel = new PostRedirectModel(); @* diff --git a/src/Umbraco.Web.UI/Umbraco/PartialViewMacros/Templates/RegisterMember.cshtml b/src/Umbraco.Web.UI/Umbraco/PartialViewMacros/Templates/RegisterMember.cshtml index 804e2307f0..81389f4a3d 100644 --- a/src/Umbraco.Web.UI/Umbraco/PartialViewMacros/Templates/RegisterMember.cshtml +++ b/src/Umbraco.Web.UI/Umbraco/PartialViewMacros/Templates/RegisterMember.cshtml @@ -1,6 +1,7 @@ @using System.Web.Mvc.Html @using ClientDependency.Core.Mvc @using Umbraco.Web +@using Umbraco.Web.Composing @using Umbraco.Web.Controllers @inherits Umbraco.Web.Macros.PartialViewMacroPage @@ -12,7 +13,7 @@ var registerModel = Members.CreateRegistrationModel("Custom Member"); *@ - var registerModel = Members.CreateRegistrationModel(); + var registerModel = Current.MembershipHelper.CreateRegistrationModel(); @* Configurable here: diff --git a/src/Umbraco.Web/AspNet/AspNetUmbracoApplicationLifetime.cs b/src/Umbraco.Web/AspNet/AspNetUmbracoApplicationLifetime.cs index 7651365c74..245e8ea374 100644 --- a/src/Umbraco.Web/AspNet/AspNetUmbracoApplicationLifetime.cs +++ b/src/Umbraco.Web/AspNet/AspNetUmbracoApplicationLifetime.cs @@ -1,3 +1,5 @@ +using System.Threading; +using System.Web; using Umbraco.Net; namespace Umbraco.Web.AspNet @@ -11,9 +13,22 @@ namespace Umbraco.Web.AspNet _httpContextAccessor = httpContextAccessor; } + public bool IsRestarting { get; set; } + public void Restart() { - UmbracoApplication.Restart(_httpContextAccessor.HttpContext); + IsRestarting = true; + + var httpContext = _httpContextAccessor.HttpContext; + if (httpContext != null) + { + // unload app domain - we must null out all identities otherwise we get serialization errors + // http://www.zpqrtbnk.net/posts/custom-iidentity-serialization-issue + httpContext.User = null; + } + + Thread.CurrentPrincipal = null; + HttpRuntime.UnloadAppDomain(); } } } diff --git a/src/Umbraco.Web/Composing/Current.cs b/src/Umbraco.Web/Composing/Current.cs index 8d4af6703e..47c36177fe 100644 --- a/src/Umbraco.Web/Composing/Current.cs +++ b/src/Umbraco.Web/Composing/Current.cs @@ -26,6 +26,7 @@ using Umbraco.Web.HealthCheck; using Umbraco.Web.Mvc; using Umbraco.Web.PublishedCache; using Umbraco.Web.Routing; +using Umbraco.Web.Security; using Umbraco.Web.Services; using Umbraco.Web.Trees; using Umbraco.Web.WebApi; @@ -262,6 +263,8 @@ namespace Umbraco.Web.Composing public static IUmbracoVersion UmbracoVersion => Factory.GetInstance(); public static IPublishedUrlProvider PublishedUrlProvider => Factory.GetInstance(); public static IMenuItemCollectionFactory MenuItemCollectionFactory => Factory.GetInstance(); + public static MembershipHelper MembershipHelper => Factory.GetInstance(); + public static IUmbracoApplicationLifetime UmbracoApplicationLifetime => Factory.GetInstance(); #endregion } diff --git a/src/Umbraco.Web/Editors/PackageInstallController.cs b/src/Umbraco.Web/Editors/PackageInstallController.cs index 4563547346..a0ab2de68a 100644 --- a/src/Umbraco.Web/Editors/PackageInstallController.cs +++ b/src/Umbraco.Web/Editors/PackageInstallController.cs @@ -18,6 +18,7 @@ using Umbraco.Core.Packaging; using Umbraco.Core.Persistence; using Umbraco.Core.Services; using Umbraco.Core.Strings; +using Umbraco.Net; using Umbraco.Web.JavaScript; using Umbraco.Web.Models; using Umbraco.Web.Models.ContentEditing; @@ -39,6 +40,7 @@ namespace Umbraco.Web.Editors private readonly IUmbracoVersion _umbracoVersion; private readonly IIOHelper _ioHelper; + private readonly IUmbracoApplicationLifetime _umbracoApplicationLifetime; public PackageInstallController( IGlobalSettings globalSettings, @@ -53,11 +55,13 @@ namespace Umbraco.Web.Editors IUmbracoVersion umbracoVersion, UmbracoMapper umbracoMapper, IIOHelper ioHelper, - IPublishedUrlProvider publishedUrlProvider) + IPublishedUrlProvider publishedUrlProvider, + IUmbracoApplicationLifetime umbracoApplicationLifetime) : base(globalSettings, umbracoContextAccessor, sqlContext, services, appCaches, logger, runtimeState, umbracoHelper, shortStringHelper, umbracoMapper, publishedUrlProvider) { _umbracoVersion = umbracoVersion; _ioHelper = ioHelper; + _umbracoApplicationLifetime = umbracoApplicationLifetime; } /// @@ -325,7 +329,7 @@ namespace Umbraco.Web.Editors var installedFiles = Services.PackagingService.InstallCompiledPackageFiles(definition, zipFile, Security.GetUserId().ResultOr(0)); //set a restarting marker and reset the app pool - UmbracoApplication.Restart(Request.TryGetHttpContext().Result); + _umbracoApplicationLifetime.Restart(); model.IsRestarting = true; @@ -337,12 +341,8 @@ namespace Umbraco.Web.Editors { if (model.IsRestarting == false) return model; - //check for the key, if it's not there we're are restarted - if (Request.TryGetHttpContext().Result.Application.AllKeys.Contains("AppPoolRestarting") == false) - { - //reset it - model.IsRestarting = false; - } + model.IsRestarting = _umbracoApplicationLifetime.IsRestarting; + return model; } diff --git a/src/Umbraco.Web/Mvc/ContentModelBinder.cs b/src/Umbraco.Web/Mvc/ContentModelBinder.cs index 052938807a..a66f8e9089 100644 --- a/src/Umbraco.Web/Mvc/ContentModelBinder.cs +++ b/src/Umbraco.Web/Mvc/ContentModelBinder.cs @@ -151,9 +151,15 @@ namespace Umbraco.Web.Mvc var context = HttpContext.Current; if (context == null) + { AppDomain.Unload(AppDomain.CurrentDomain); + } else - UmbracoApplication.Restart(new HttpContextWrapper(context)); + { + Current.UmbracoApplicationLifetime.Restart(); + } + + } throw new ModelBindingException(msg.ToString()); diff --git a/src/Umbraco.Web/Runtime/WebInitialComposer.cs b/src/Umbraco.Web/Runtime/WebInitialComposer.cs index 34fb6413be..e4b50a248c 100644 --- a/src/Umbraco.Web/Runtime/WebInitialComposer.cs +++ b/src/Umbraco.Web/Runtime/WebInitialComposer.cs @@ -69,7 +69,7 @@ namespace Umbraco.Web.Runtime composition.Register(); composition.Register(); composition.Register(); - composition.Register(); + composition.Register(Lifetime.Singleton); composition.Register(); composition.Register(Lifetime.Singleton); diff --git a/src/Umbraco.Web/UmbracoApplication.cs b/src/Umbraco.Web/UmbracoApplication.cs index 90d575bf73..098fc94724 100644 --- a/src/Umbraco.Web/UmbracoApplication.cs +++ b/src/Umbraco.Web/UmbracoApplication.cs @@ -36,46 +36,5 @@ namespace Umbraco.Web return new WebRuntime(this, configs, umbracoVersion, ioHelper, logger, profiler, hostingEnvironment, backOfficeInfo, dbProviderFactoryCreator, mainDom); } - - /// - /// Restarts the Umbraco application. - /// - public static void Restart() - { - // see notes in overload - - var httpContext = HttpContext.Current; - if (httpContext != null) - { - httpContext.Application.Add("AppPoolRestarting", true); - httpContext.User = null; - } - Thread.CurrentPrincipal = null; - HttpRuntime.UnloadAppDomain(); - } - - /// - /// Restarts the Umbraco application. - /// - public static void Restart(HttpContextBase httpContext) - { - if (httpContext != null) - { - // we're going to put an application wide flag to show that the application is about to restart. - // we're doing this because if there is a script checking if the app pool is fully restarted, then - // it can check if this flag exists... if it does it means the app pool isn't restarted yet. - httpContext.Application.Add("AppPoolRestarting", true); - - // unload app domain - we must null out all identities otherwise we get serialization errors - // http://www.zpqrtbnk.net/posts/custom-iidentity-serialization-issue - httpContext.User = null; - } - - if (HttpContext.Current != null) - HttpContext.Current.User = null; - - Thread.CurrentPrincipal = null; - HttpRuntime.UnloadAppDomain(); - } } }