More work, but still WIP
This commit is contained in:
parent
e39ee05314
commit
e9a054d743
|
@ -3,6 +3,9 @@ using Microsoft.AspNetCore.Http.Extensions;
|
|||
|
||||
namespace BitBadger.AspNetCore.CanonicalDomains;
|
||||
|
||||
/// <summary>
|
||||
/// Middleware to enforce canonical domains
|
||||
/// </summary>
|
||||
public class CanonicalDomainMiddleware
|
||||
{
|
||||
/// <summary>
|
||||
|
@ -26,12 +29,11 @@ public class CanonicalDomainMiddleware
|
|||
|
||||
public async Task InvokeAsync(HttpContext ctx)
|
||||
{
|
||||
var host = ctx.Request.Host.Host;
|
||||
if (CanonicalDomains.ContainsKey(host))
|
||||
if (CanonicalDomains.ContainsKey(ctx.Request.Host.Host))
|
||||
{
|
||||
UriBuilder uri = new(ctx.Request.GetDisplayUrl());
|
||||
uri.Host = CanonicalDomains[host];
|
||||
ctx.Response.Redirect(uri.Uri.ToString ());
|
||||
uri.Host = CanonicalDomains[ctx.Request.Host.Host];
|
||||
ctx.Response.Redirect(uri.Uri.ToString(), permanent: true);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
@ -14,18 +14,31 @@ public static class IApplicationBuilderExtensions
|
|||
/// </summary>
|
||||
public static IApplicationBuilder UseCanonicalDomains(this IApplicationBuilder app)
|
||||
{
|
||||
void warnForMissingConfig() {
|
||||
var logger = (ILogger<CanonicalDomainMiddleware>?)app.ApplicationServices
|
||||
.GetService(typeof(ILogger<CanonicalDomainMiddleware>));
|
||||
if (logger is not null)
|
||||
{
|
||||
logger.LogWarning("No canonical domain configuration was found; no domains will be redirected");
|
||||
}
|
||||
ParseConfiguration(GetService<IConfiguration>(app)!.GetSection("CanonicalDomains"));
|
||||
|
||||
if (CanonicalDomainMiddleware.CanonicalDomains.Count > 0)
|
||||
{
|
||||
return app.UseMiddleware<CanonicalDomainMiddleware>();
|
||||
}
|
||||
|
||||
var config = (IConfiguration)app.ApplicationServices.GetService(typeof(IConfiguration))!;
|
||||
WarnForMissingConfig(app);
|
||||
return app;
|
||||
}
|
||||
|
||||
var section = config.GetSection("CanonicalDomains");
|
||||
/// <summary>
|
||||
/// Shorthand for retrieving typed services from the application's service provider
|
||||
/// </summary>
|
||||
/// <param name="app">The application builder</param>
|
||||
/// <returns>The requested service, or null if it was not able to be found</returns>
|
||||
private static T? GetService<T>(IApplicationBuilder app) =>
|
||||
(T?)app.ApplicationServices.GetService(typeof(T));
|
||||
|
||||
/// <summary>
|
||||
/// Extract the from/to domain paris from the configuration
|
||||
/// </summary>
|
||||
/// <param name="section">The <tt>CanonicalDomains</tt> configuration section</param>
|
||||
private static void ParseConfiguration(IConfigurationSection? section)
|
||||
{
|
||||
if (section is not null)
|
||||
{
|
||||
foreach (var item in section.GetChildren())
|
||||
|
@ -37,21 +50,19 @@ public static class IApplicationBuilderExtensions
|
|||
CanonicalDomainMiddleware.CanonicalDomains.Add(nonCanonical, canonical);
|
||||
}
|
||||
}
|
||||
|
||||
if (CanonicalDomainMiddleware.CanonicalDomains.Count > 0)
|
||||
{
|
||||
app.UseMiddleware<CanonicalDomainMiddleware> ();
|
||||
}
|
||||
else
|
||||
{
|
||||
warnForMissingConfig();
|
||||
}
|
||||
}
|
||||
else
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generate a warning if no configured domains were found
|
||||
/// </summary>
|
||||
/// <param name="app">The application builder</param>
|
||||
private static void WarnForMissingConfig(IApplicationBuilder app)
|
||||
{
|
||||
var logger = GetService<ILogger<CanonicalDomainMiddleware>>(app);
|
||||
if (logger is not null)
|
||||
{
|
||||
warnForMissingConfig();
|
||||
logger.LogWarning("No canonical domain configuration was found; no domains will be redirected");
|
||||
}
|
||||
|
||||
return app;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user