From a329fb1a9232f0ac3035914e45705abcbee85a1a Mon Sep 17 00:00:00 2001 From: Bjarke Berg Date: Thu, 13 Feb 2020 08:52:58 +0100 Subject: [PATCH] Replaced HttpContextBase injection with HttpContextAccessor injection --- .../Install/InstallSteps/NewInstallStep.cs | 23 +++++++++---------- .../InstallSteps/SetUmbracoVersionStep.cs | 8 +++---- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/Umbraco.Web/Install/InstallSteps/NewInstallStep.cs b/src/Umbraco.Web/Install/InstallSteps/NewInstallStep.cs index 9a451ba010..6122e2924a 100644 --- a/src/Umbraco.Web/Install/InstallSteps/NewInstallStep.cs +++ b/src/Umbraco.Web/Install/InstallSteps/NewInstallStep.cs @@ -29,26 +29,24 @@ namespace Umbraco.Web.Install.InstallSteps [InstallSetupStep(InstallationType.NewInstall, "User", 20, "")] internal class NewInstallStep : InstallSetupStep { - private readonly HttpContextBase _http; + private readonly IHttpContextAccessor _httpContextAccessor; private readonly IUserService _userService; private readonly DatabaseBuilder _databaseBuilder; private static HttpClient _httpClient; private readonly IGlobalSettings _globalSettings; private readonly IUserPasswordConfiguration _passwordConfiguration; - private readonly BackOfficeUserManager _userManager; private readonly IUmbracoSettingsSection _umbracoSettingsSection; private readonly IConnectionStrings _connectionStrings; - public NewInstallStep(HttpContextBase http, IUserService userService, DatabaseBuilder databaseBuilder, IGlobalSettings globalSettings, IUserPasswordConfiguration passwordConfiguration, IUmbracoSettingsSection umbracoSettingsSection, IConnectionStrings connectionStrings) + public NewInstallStep(IHttpContextAccessor httpContextAccessor, IUserService userService, DatabaseBuilder databaseBuilder, IGlobalSettings globalSettings, IUserPasswordConfiguration passwordConfiguration, IUmbracoSettingsSection umbracoSettingsSection, IConnectionStrings connectionStrings) { - _http = http; - _userService = userService; - _databaseBuilder = databaseBuilder; - _globalSettings = globalSettings; + _httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor)); + _userService = userService ?? throw new ArgumentNullException(nameof(userService)); + _databaseBuilder = databaseBuilder ?? throw new ArgumentNullException(nameof(databaseBuilder)); + _globalSettings = globalSettings ?? throw new ArgumentNullException(nameof(globalSettings)); _passwordConfiguration = passwordConfiguration ?? throw new ArgumentNullException(nameof(passwordConfiguration)); _umbracoSettingsSection = umbracoSettingsSection ?? throw new ArgumentNullException(nameof(umbracoSettingsSection)); _connectionStrings = connectionStrings ?? throw new ArgumentNullException(nameof(connectionStrings)); - _userManager = _http.GetOwinContext().GetBackOfficeUserManager(); } public override async Task ExecuteAsync(UserModel user) @@ -59,15 +57,16 @@ namespace Umbraco.Web.Install.InstallSteps throw new InvalidOperationException("Could not find the super user!"); } - var membershipUser = await _userManager.FindByIdAsync(Constants.Security.SuperUserId); + var userManager = _httpContextAccessor.HttpContext.GetOwinContext().GetBackOfficeUserManager(); + var membershipUser = await userManager.FindByIdAsync(Constants.Security.SuperUserId); if (membershipUser == null) { throw new InvalidOperationException($"No user found in membership provider with id of {Constants.Security.SuperUserId}."); } //To change the password here we actually need to reset it since we don't have an old one to use to change - var resetToken = await _userManager.GeneratePasswordResetTokenAsync(membershipUser.Id); - var resetResult = await _userManager.ChangePasswordWithResetAsync(membershipUser.Id, resetToken, user.Password.Trim()); + var resetToken = await userManager.GeneratePasswordResetTokenAsync(membershipUser.Id); + var resetResult = await userManager.ChangePasswordWithResetAsync(membershipUser.Id, resetToken, user.Password.Trim()); if (!resetResult.Succeeded) { throw new InvalidOperationException("Could not reset password: " + string.Join(", ", resetResult.Errors)); @@ -138,7 +137,7 @@ namespace Umbraco.Web.Install.InstallSteps // In this one case when it's a brand new install and nothing has been configured, make sure the // back office cookie is cleared so there's no old cookies lying around causing problems - _http.ExpireCookie(_umbracoSettingsSection.Security.AuthCookieName); + _httpContextAccessor.HttpContext.ExpireCookie(_umbracoSettingsSection.Security.AuthCookieName); return true; } diff --git a/src/Umbraco.Web/Install/InstallSteps/SetUmbracoVersionStep.cs b/src/Umbraco.Web/Install/InstallSteps/SetUmbracoVersionStep.cs index 63247f5f0b..6b9e2f392e 100644 --- a/src/Umbraco.Web/Install/InstallSteps/SetUmbracoVersionStep.cs +++ b/src/Umbraco.Web/Install/InstallSteps/SetUmbracoVersionStep.cs @@ -18,16 +18,16 @@ namespace Umbraco.Web.Install.InstallSteps PerformsAppRestart = true)] internal class SetUmbracoVersionStep : InstallSetupStep { - private readonly HttpContextBase _httpContext; + private readonly IHttpContextAccessor _httpContextAccessor; private readonly InstallHelper _installHelper; private readonly IGlobalSettings _globalSettings; private readonly IUserService _userService; private readonly IUmbracoVersion _umbracoVersion; private readonly IIOHelper _ioHelper; - public SetUmbracoVersionStep(HttpContextBase httpContext, InstallHelper installHelper, IGlobalSettings globalSettings, IUserService userService, IUmbracoVersion umbracoVersion, IIOHelper ioHelper) + public SetUmbracoVersionStep(IHttpContextAccessor httpContextAccessor, InstallHelper installHelper, IGlobalSettings globalSettings, IUserService userService, IUmbracoVersion umbracoVersion, IIOHelper ioHelper) { - _httpContext = httpContext; + _httpContextAccessor = httpContextAccessor; _installHelper = installHelper; _globalSettings = globalSettings; _userService = userService; @@ -37,7 +37,7 @@ namespace Umbraco.Web.Install.InstallSteps public override Task ExecuteAsync(object model) { - var security = new WebSecurity(_httpContext, _userService, _globalSettings, _ioHelper); + var security = new WebSecurity(_httpContextAccessor.HttpContext, _userService, _globalSettings, _ioHelper); if (security.IsAuthenticated() == false && _globalSettings.ConfigurationStatus.IsNullOrWhiteSpace()) {