diff --git a/Our.Umbraco.MaintenanceMode/Middleware/MaintenanceRedirectMiddleware.cs b/Our.Umbraco.MaintenanceMode/Middleware/MaintenanceRedirectMiddleware.cs index 591d21b..ead8ff9 100644 --- a/Our.Umbraco.MaintenanceMode/Middleware/MaintenanceRedirectMiddleware.cs +++ b/Our.Umbraco.MaintenanceMode/Middleware/MaintenanceRedirectMiddleware.cs @@ -33,18 +33,23 @@ public async Task InvokeAsync(HttpContext context, IBackofficeUserAccessor backo { _logger.LogDebug("Maintenance mode middleware triggered {url}", context.Request.Path); - if (_runtimeState.Level == RuntimeLevel.Run && - _maintenanceModeService.IsInMaintenanceMode) + bool IsAuthenticated = IsBackOfficeAuthenticated(backofficeUserAccessor); + + if (backofficeUserAccessor != null) { - // todo figure out how to do this check here - if (!_maintenanceModeService.Settings.AllowBackOfficeUsersThrough - && backofficeUserAccessor.BackofficeUser.IsAuthenticated) - { - context = HandleRequest(context); - } - else if (!backofficeUserAccessor.BackofficeUser.IsAuthenticated) + if (_runtimeState.Level == RuntimeLevel.Run && + _maintenanceModeService.IsInMaintenanceMode) { - context = HandleRequest(context); + // todo figure out how to do this check here + if (!_maintenanceModeService.Settings.AllowBackOfficeUsersThrough + && IsAuthenticated) + { + context = HandleRequest(context); + } + else if (!IsAuthenticated) + { + context = HandleRequest(context); + } } } @@ -53,6 +58,17 @@ public async Task InvokeAsync(HttpContext context, IBackofficeUserAccessor backo } + private bool IsBackOfficeAuthenticated(IBackofficeUserAccessor backofficeUserAccessor) { + try { + return backofficeUserAccessor.BackofficeUser?.IsAuthenticated ?? false; + } + catch(System.Exception ex) { + // in v10 - this thows an erro internally, if there is no backoffice user 🤷‍♀️ + // _logger.LogWarning(ex, "Error getting back office user"); + return false; + } + } + private static HttpContext HandleRequest(HttpContext context) { var newPath = new PathString($"/{MaintenanceMode.MaintenanceRoot}");