From 09645149a81a34ffadd989ebfdb2ec15af0579a0 Mon Sep 17 00:00:00 2001 From: James Griffiths Date: Mon, 4 Nov 2024 13:57:19 +0000 Subject: [PATCH] EHD-1411: Reduce code in Core project: Move middlewares --- .../Helpers}/MaintenancePageMiddleware.cs | 77 +++++++-------- .../Helpers}/SecurityHeaderMiddleware.cs | 94 +++++++++---------- 2 files changed, 86 insertions(+), 85 deletions(-) rename {GenderPayGap.Core/Extensions/AspNetCore => GenderPayGap.WebUI/Helpers}/MaintenancePageMiddleware.cs (92%) rename {GenderPayGap.Core/Extensions/AspNetCore => GenderPayGap.WebUI/Helpers}/SecurityHeaderMiddleware.cs (94%) diff --git a/GenderPayGap.Core/Extensions/AspNetCore/MaintenancePageMiddleware.cs b/GenderPayGap.WebUI/Helpers/MaintenancePageMiddleware.cs similarity index 92% rename from GenderPayGap.Core/Extensions/AspNetCore/MaintenancePageMiddleware.cs rename to GenderPayGap.WebUI/Helpers/MaintenancePageMiddleware.cs index befc53b4d..42292d022 100644 --- a/GenderPayGap.Core/Extensions/AspNetCore/MaintenancePageMiddleware.cs +++ b/GenderPayGap.WebUI/Helpers/MaintenancePageMiddleware.cs @@ -1,38 +1,39 @@ -using System.Threading.Tasks; -using Microsoft.AspNetCore.Http; -using HttpContext = Microsoft.AspNetCore.Http.HttpContext; - -namespace GenderPayGap.Extensions.AspNetCore -{ - public class MaintenancePageMiddleware - { - - private readonly bool _enabled; - private readonly RequestDelegate _next; - - public MaintenancePageMiddleware(RequestDelegate next, bool enabled) - { - _next = next; - _enabled = enabled; - } - - - public async Task Invoke(HttpContext httpContext) - { - if (httpContext.Request.Path.Value.StartsWith("/health-check")) - { - await _next.Invoke(httpContext); - return; - } - - // Redirect to holding page if in maintenance mode - if (_enabled && !httpContext.Request.Path.Value.StartsWithI(@"/error/service-unavailable")) - { - httpContext.Response.Redirect(@"/error/service-unavailable", permanent: false); - } - - await _next.Invoke(httpContext); - } - - } -} +using System.Threading.Tasks; +using GenderPayGap.Extensions; +using Microsoft.AspNetCore.Http; +using HttpContext = Microsoft.AspNetCore.Http.HttpContext; + +namespace GenderPayGap.WebUI.Helpers +{ + public class MaintenancePageMiddleware + { + + private readonly bool _enabled; + private readonly RequestDelegate _next; + + public MaintenancePageMiddleware(RequestDelegate next, bool enabled) + { + _next = next; + _enabled = enabled; + } + + + public async Task Invoke(HttpContext httpContext) + { + if (httpContext.Request.Path.Value.StartsWith("/health-check")) + { + await _next.Invoke(httpContext); + return; + } + + // Redirect to holding page if in maintenance mode + if (_enabled && !httpContext.Request.Path.Value.StartsWithI(@"/error/service-unavailable")) + { + httpContext.Response.Redirect(@"/error/service-unavailable", permanent: false); + } + + await _next.Invoke(httpContext); + } + + } +} diff --git a/GenderPayGap.Core/Extensions/AspNetCore/SecurityHeaderMiddleware.cs b/GenderPayGap.WebUI/Helpers/SecurityHeaderMiddleware.cs similarity index 94% rename from GenderPayGap.Core/Extensions/AspNetCore/SecurityHeaderMiddleware.cs rename to GenderPayGap.WebUI/Helpers/SecurityHeaderMiddleware.cs index e18cb4f29..efb84a868 100644 --- a/GenderPayGap.Core/Extensions/AspNetCore/SecurityHeaderMiddleware.cs +++ b/GenderPayGap.WebUI/Helpers/SecurityHeaderMiddleware.cs @@ -1,47 +1,47 @@ -using System.Collections.Generic; -using System.Threading.Tasks; -using GenderPayGap.Core; -using Microsoft.AspNetCore.Http; - -namespace GenderPayGap.Extensions.AspNetCore -{ - public class SecurityHeaderMiddleware - { - - private readonly RequestDelegate _next; - - public SecurityHeaderMiddleware(RequestDelegate next) - { - _next = next; - } - - public async Task Invoke(HttpContext httpContext) - { - httpContext.Response.OnStarting( - () => { - foreach (KeyValuePair securityHeader in Global.SecurityHeadersToAdd) - { - if (!httpContext.Response.Headers.ContainsKey(securityHeader.Key)) - { - httpContext.Response.Headers.Add(securityHeader.Key, securityHeader.Value); - } - else if (httpContext.Response.Headers[securityHeader.Key] != securityHeader.Value) - { - httpContext.Response.Headers.Remove(securityHeader.Key); // This is required as we cannot change a key once it is added - httpContext.Response.Headers[securityHeader.Key] = securityHeader.Value; - } - } - - foreach (string securityHeaderName in Global.SecurityHeadersToRemove) - { - httpContext.Response.Headers.Remove(securityHeaderName); - } - - return Task.CompletedTask; - }); - - await _next.Invoke(httpContext); - } - - } -} +using System.Collections.Generic; +using System.Threading.Tasks; +using GenderPayGap.Core; +using Microsoft.AspNetCore.Http; + +namespace GenderPayGap.WebUI.Helpers +{ + public class SecurityHeaderMiddleware + { + + private readonly RequestDelegate _next; + + public SecurityHeaderMiddleware(RequestDelegate next) + { + _next = next; + } + + public async Task Invoke(HttpContext httpContext) + { + httpContext.Response.OnStarting( + () => { + foreach (KeyValuePair securityHeader in Global.SecurityHeadersToAdd) + { + if (!httpContext.Response.Headers.ContainsKey(securityHeader.Key)) + { + httpContext.Response.Headers.Add(securityHeader.Key, securityHeader.Value); + } + else if (httpContext.Response.Headers[securityHeader.Key] != securityHeader.Value) + { + httpContext.Response.Headers.Remove(securityHeader.Key); // This is required as we cannot change a key once it is added + httpContext.Response.Headers[securityHeader.Key] = securityHeader.Value; + } + } + + foreach (string securityHeaderName in Global.SecurityHeadersToRemove) + { + httpContext.Response.Headers.Remove(securityHeaderName); + } + + return Task.CompletedTask; + }); + + await _next.Invoke(httpContext); + } + + } +}