Update .NET versions; remove deprecated package references
This commit is contained in:
parent
69d4122c1c
commit
daa5629d1b
2
.gitignore
vendored
2
.gitignore
vendored
@ -395,3 +395,5 @@ FodyWeavers.xsd
|
|||||||
|
|
||||||
# JetBrains Rider
|
# JetBrains Rider
|
||||||
*.sln.iml
|
*.sln.iml
|
||||||
|
.idea/
|
||||||
|
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
|
<TargetFrameworks>net6.0;net8.0;net9.0</TargetFrameworks>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<VersionPrefix>1.0.0</VersionPrefix>
|
<VersionPrefix>1.1.0</VersionPrefix>
|
||||||
<PackageReleaseNotes>Initial release</PackageReleaseNotes>
|
<PackageReleaseNotes>Update supported .NET versions; remove deprecated package references</PackageReleaseNotes>
|
||||||
<Authors>danieljsummers</Authors>
|
<Authors>danieljsummers</Authors>
|
||||||
<Company>Bit Badger Solutions</Company>
|
<Company>Bit Badger Solutions</Company>
|
||||||
<Description>ASP.NET Core middleware to enforce canonical domains</Description>
|
<Description>ASP.NET Core middleware to enforce canonical domains</Description>
|
||||||
@ -26,8 +26,6 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
|
<FrameworkReference Include="Microsoft.AspNetCore.App" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
|
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Http.Extensions" Version="2.2.0" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
@ -21,7 +21,7 @@ public class CanonicalDomainMiddleware
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Constructor
|
/// Constructor
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="next">The next middleware in the pipeline to be exectued</param>
|
/// <param name="next">The next middleware in the pipeline to be executed</param>
|
||||||
public CanonicalDomainMiddleware(RequestDelegate next)
|
public CanonicalDomainMiddleware(RequestDelegate next)
|
||||||
{
|
{
|
||||||
_next = next;
|
_next = next;
|
||||||
@ -29,10 +29,12 @@ public class CanonicalDomainMiddleware
|
|||||||
|
|
||||||
public async Task InvokeAsync(HttpContext ctx)
|
public async Task InvokeAsync(HttpContext ctx)
|
||||||
{
|
{
|
||||||
if (CanonicalDomains.ContainsKey(ctx.Request.Host.Host))
|
if (CanonicalDomains.TryGetValue(ctx.Request.Host.Host, out var value))
|
||||||
{
|
{
|
||||||
UriBuilder uri = new(ctx.Request.GetDisplayUrl());
|
UriBuilder uri = new(ctx.Request.GetDisplayUrl())
|
||||||
uri.Host = CanonicalDomains[ctx.Request.Host.Host];
|
{
|
||||||
|
Host = value
|
||||||
|
};
|
||||||
ctx.Response.Redirect(uri.Uri.ToString(), permanent: true);
|
ctx.Response.Redirect(uri.Uri.ToString(), permanent: true);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -39,16 +39,14 @@ public static class IApplicationBuilderExtensions
|
|||||||
/// <param name="section">The <tt>CanonicalDomains</tt> configuration section</param>
|
/// <param name="section">The <tt>CanonicalDomains</tt> configuration section</param>
|
||||||
private static void ParseConfiguration(IConfigurationSection? section)
|
private static void ParseConfiguration(IConfigurationSection? section)
|
||||||
{
|
{
|
||||||
if (section is not null)
|
if (section is null) return;
|
||||||
|
foreach (var item in section.GetChildren())
|
||||||
{
|
{
|
||||||
foreach (var item in section.GetChildren())
|
var nonCanonical = item["From"];
|
||||||
|
var canonical = item["To"];
|
||||||
|
if (nonCanonical is not null && canonical is not null)
|
||||||
{
|
{
|
||||||
var nonCanonical = item["From"];
|
CanonicalDomainMiddleware.CanonicalDomains.Add(nonCanonical, canonical);
|
||||||
var canonical = item["To"];
|
|
||||||
if (nonCanonical is not null && canonical is not null)
|
|
||||||
{
|
|
||||||
CanonicalDomainMiddleware.CanonicalDomains.Add(nonCanonical, canonical);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -57,12 +55,7 @@ public static class IApplicationBuilderExtensions
|
|||||||
/// Generate a warning if no configured domains were found
|
/// Generate a warning if no configured domains were found
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="app">The application builder</param>
|
/// <param name="app">The application builder</param>
|
||||||
private static void WarnForMissingConfig(IApplicationBuilder app)
|
private static void WarnForMissingConfig(IApplicationBuilder app) =>
|
||||||
{
|
GetService<ILogger<CanonicalDomainMiddleware>>(app)?
|
||||||
var logger = GetService<ILogger<CanonicalDomainMiddleware>>(app);
|
.LogWarning("No canonical domain configuration was found; no domains will be redirected");
|
||||||
if (logger is not null)
|
|
||||||
{
|
|
||||||
logger.LogWarning("No canonical domain configuration was found; no domains will be redirected");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user