-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
EHD-1411: Reduce code in Core project: Move middlewares
1 parent
619b59f
commit 0964514
Showing
2 changed files
with
86 additions
and
85 deletions.
There are no files selected for viewing
77 changes: 39 additions & 38 deletions
77
...s/AspNetCore/MaintenancePageMiddleware.cs → ...ebUI/Helpers/MaintenancePageMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} | ||
} |
94 changes: 47 additions & 47 deletions
94
...ns/AspNetCore/SecurityHeaderMiddleware.cs → ...WebUI/Helpers/SecurityHeaderMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<string, string> 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<string, string> 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); | ||
} | ||
|
||
} | ||
} |