Skip to content

Commit 1a939be

Browse files
committed
update solution to .NET 6
1 parent 2298085 commit 1a939be

9 files changed

+31
-50
lines changed

MyWebApp.IntegrationTests/CustomRemoteIpAddressMiddleware.cs

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
using System.Net;
3-
using System.Threading.Tasks;
1+
using System.Net;
42
using Microsoft.AspNetCore.Builder;
53
using Microsoft.AspNetCore.Hosting;
64
using Microsoft.AspNetCore.Http;
@@ -12,7 +10,7 @@ public class CustomRemoteIpAddressMiddleware
1210
private readonly RequestDelegate _next;
1311
private readonly IPAddress _fakeIpAddress;
1412

15-
public CustomRemoteIpAddressMiddleware(RequestDelegate next, IPAddress fakeIpAddress = null)
13+
public CustomRemoteIpAddressMiddleware(RequestDelegate next, IPAddress? fakeIpAddress = null)
1614
{
1715
_next = next;
1816
_fakeIpAddress = fakeIpAddress ?? IPAddress.Parse("127.0.0.1");
@@ -27,9 +25,9 @@ public async Task Invoke(HttpContext httpContext)
2725

2826
public class CustomRemoteIpStartupFilter : IStartupFilter
2927
{
30-
private readonly IPAddress _remoteIp;
28+
private readonly IPAddress? _remoteIp;
3129

32-
public CustomRemoteIpStartupFilter(IPAddress remoteIp = null)
30+
public CustomRemoteIpStartupFilter(IPAddress? remoteIp = null)
3331
{
3432
_remoteIp = remoteIp;
3533
}

MyWebApp.IntegrationTests/IpRestrictionTests.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using System.Net;
2-
using System.Threading.Tasks;
32
using Microsoft.AspNetCore.Hosting;
3+
using Microsoft.AspNetCore.Http;
44
using Microsoft.AspNetCore.Mvc.Testing;
55
using Microsoft.AspNetCore.TestHost;
66
using Microsoft.Extensions.DependencyInjection;
7+
using Microsoft.Extensions.Logging;
78
using Microsoft.VisualStudio.TestTools.UnitTesting;
89

910
namespace MyWebApp.IntegrationTests
@@ -16,7 +17,7 @@ public async Task HttpRequestWithAllowedIpAddressShouldReturn200()
1617
{
1718
var factory = new WebApplicationFactory<Startup>().WithWebHostBuilder(builder =>
1819
{
19-
builder.UseSetting("https_port", "5001");
20+
builder.UseSetting("https_port", "5001").ConfigureLogging(c => c.AddConsole());
2021
builder.ConfigureTestServices(services =>
2122
{
2223
services.AddSingleton<IStartupFilter>(new CustomRemoteIpStartupFilter(IPAddress.Parse("127.0.0.1")));
@@ -37,7 +38,7 @@ public async Task HttpRequestWithForbiddenIpAddressShouldReturn403()
3738
{
3839
var factory = new WebApplicationFactory<Startup>().WithWebHostBuilder(builder =>
3940
{
40-
builder.UseSetting("https_port", "5001");
41+
builder.UseSetting("https_port", "5001").ConfigureLogging(c => c.AddConsole());
4142
builder.ConfigureTestServices(services =>
4243
{
4344
services.AddSingleton<IStartupFilter>(new CustomRemoteIpStartupFilter(IPAddress.Parse("127.168.1.32")));
@@ -54,7 +55,7 @@ public async Task HttpRequestWithForbiddenIpAddressShouldReturn403()
5455
public async Task HttpRequestWithLocalHostIpAddressShouldReturn200()
5556
{
5657
var factory = new WebApplicationFactory<Startup>()
57-
.WithWebHostBuilder(builder => builder.UseSetting("https_port", "5001"));
58+
.WithWebHostBuilder(builder => builder.UseSetting("https_port", "5001").ConfigureLogging(c => c.AddConsole()));
5859
var client = factory.CreateClient(new WebApplicationFactoryClientOptions
5960
{
6061
AllowAutoRedirect = true

MyWebApp.IntegrationTests/MyWebApp.IntegrationTests.csproj

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net5.0</TargetFramework>
4+
<TargetFramework>net6.0</TargetFramework>
55

66
<IsPackable>false</IsPackable>
7+
8+
<Nullable>enable</Nullable>
9+
10+
<ImplicitUsings>enable</ImplicitUsings>
711
</PropertyGroup>
812

913
<ItemGroup>
10-
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="5.0.4" />
11-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1" />
12-
<PackageReference Include="MSTest.TestAdapter" Version="2.2.1" />
13-
<PackageReference Include="MSTest.TestFramework" Version="2.2.1" />
14-
<PackageReference Include="coverlet.collector" Version="3.0.3">
15-
<PrivateAssets>all</PrivateAssets>
16-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
17-
</PackageReference>
14+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.8" />
15+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
16+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.1" />
17+
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
18+
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
1819
</ItemGroup>
1920

2021
<ItemGroup>

MyWebApp/Controllers/ValuesController.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using Microsoft.AspNetCore.Mvc;
2-
using Microsoft.Extensions.Logging;
3-
using System.Collections.Generic;
42
using MyWebApp.Infrastructure;
53

64
namespace MyWebApp.Controllers
@@ -20,7 +18,7 @@ public ValuesController(ILogger<ValuesController> logger)
2018
[HttpGet]
2119
public IEnumerable<string> Get()
2220
{
23-
_logger.LogInformation($"Client IP: {HttpContext.Connection.RemoteIpAddress}");
21+
_logger.LogInformation("Client IP: {remoteIpAddress}", HttpContext.Connection.RemoteIpAddress?.ToString());
2422
return new[] { "value1", "value2" };
2523
}
2624
}

MyWebApp/Infrastructure/AdminSafeListMiddleware.cs

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Net;
5-
using System.Threading.Tasks;
6-
using Microsoft.AspNetCore.Http;
7-
using Microsoft.Extensions.Logging;
1+
using System.Net;
82

93
namespace MyWebApp.Infrastructure
104
{
@@ -40,7 +34,7 @@ public async Task Invoke(HttpContext context)
4034

4135
if (!_ipAddresses.Contains(remoteIp) && !_ipNetworks.Any(x => x.Contains(remoteIp)))
4236
{
43-
_logger.LogWarning($"Forbidden Request from IP: {remoteIp}");
37+
_logger.LogWarning("Forbidden Request from IP: {remoteIp}", remoteIp);
4438
context.Response.StatusCode = StatusCodes.Status403Forbidden;
4539
return;
4640
}

MyWebApp/Infrastructure/ClientIpCheckActionFilter.cs

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Net;
5-
using Microsoft.AspNetCore.Http;
1+
using System.Net;
62
using Microsoft.AspNetCore.Mvc;
73
using Microsoft.AspNetCore.Mvc.Filters;
8-
using Microsoft.Extensions.Logging;
94

105
namespace MyWebApp.Infrastructure
116
{
@@ -38,7 +33,7 @@ public override void OnActionExecuting(ActionExecutingContext context)
3833

3934
if (!_ipAddresses.Contains(remoteIp) && !_ipNetworks.Any(x => x.Contains(remoteIp)))
4035
{
41-
_logger.LogWarning($"Forbidden Request from IP: {remoteIp}");
36+
_logger.LogWarning("Forbidden Request from IP: {remoteIp}", remoteIp);
4237
context.Result = new StatusCodeResult(StatusCodes.Status403Forbidden);
4338
return;
4439
}
@@ -50,7 +45,7 @@ public override void OnActionExecuting(ActionExecutingContext context)
5045

5146
public class IpSafeList
5247
{
53-
public string IpAddresses { get; set; }
54-
public string IpNetworks { get; set; }
48+
public string IpAddresses { get; set; } = string.Empty;
49+
public string IpNetworks { get; set; } = string.Empty;
5550
}
5651
}

MyWebApp/MyWebApp.csproj

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>net5.0</TargetFramework>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
57
</PropertyGroup>
68

79
<ItemGroup>
8-
<PackageReference Include="IPNetwork2" Version="2.5.271" />
9-
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.1.0" />
10+
<PackageReference Include="IPNetwork2" Version="2.6.467" />
11+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
1012
</ItemGroup>
1113

1214
</Project>

MyWebApp/Program.cs

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
using Microsoft.AspNetCore.Hosting;
2-
using Microsoft.Extensions.Hosting;
3-
41
namespace MyWebApp
52
{
63
public class Program

MyWebApp/Startup.cs

-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
using Microsoft.AspNetCore.Builder;
2-
using Microsoft.AspNetCore.Hosting;
31
using Microsoft.AspNetCore.HttpOverrides;
4-
using Microsoft.Extensions.Configuration;
5-
using Microsoft.Extensions.DependencyInjection;
6-
using Microsoft.Extensions.Hosting;
72
using Microsoft.OpenApi.Models;
83
using MyWebApp.Infrastructure;
94

0 commit comments

Comments
 (0)