Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EHD-1411: Reduce code in Core project: Move middlewares
Browse files Browse the repository at this point in the history
jamesgriff committed Nov 4, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 619b59f commit 0964514
Showing 2 changed files with 86 additions and 85 deletions.
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);
}

}
}
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);
}

}
}

0 comments on commit 0964514

Please sign in to comment.