AspNetCore.Docs/aspnetcore/diagnostics/asp0028.md

2.8 KiB

title ms.date description author monikerRange ms.author uid
ASP0028: ## Analyzer to suggest using IPAddress.IPv6Any instead of IPAddress.Any if applicable 11/11/2024 Consider using IPAddress.IPv6Any instead of IPAddress.Any deaglegross >= aspnetcore-10.0 dmkorolev diagnostics/asp0028

ASP0028: Consider using IPAddress.IPv6Any instead of IPAddress.Any

Value
Rule ID ASP0028
Category Usage
Fix is breaking or non-breaking Non-breaking

Cause

On the server machine that supports IPv6, IPv6Any is preferred to Any because Any can be slower than IPv6Any. In some cases, Any may not work at all. Any can be slower due to the underlying System types implementation.

127.0.0.1 is the IPv4 loopback address. ::1 is the IPv6 loopback address. Any is the wildcard address for IPv4. IPv6Any is the wildcard address for IPv6.

Current behavior with with IPv6 when using HTTP/1.x or HTTP/2.0:

  • localhost resolves to [::1].
  • [::1] isn't accepted by the server, which forces a retry using 127.0.0.1, creating a repeated cycle.

Using Any with the preceding conditions causes the ASP0028 diagnostic message. Here's an example of the code that can result in these conditions:

.UseKestrel().ConfigureKestrel(options =>
{ 
    options.Listen(IPAddress.Any, ...);
})

Rule description

The recommended way to configure Kestrel to listen for incoming connections on all available IPv6 network interfaces is with IPv6Any.

How to fix violations

For the problematic code, replace Any with IPv6Any.

Use the xref:Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerOptions.ListenAnyIP(System.Int32) method without specifying any arguments:

.UseKestrel().ConfigureKestrel(options =>
{ 
-   options.Listen(IPAddress.Any, ...);
+   options.ListenAnyIP(...);
})

Or use the xref:System.Net.IPAddress.IPv6Any field:

.UseKestrel().ConfigureKestrel(options =>
{ 
-   options.Listen(IPAddress.Any, ...);
+   options.Listen(IPAddress.IPv6Any, ...);
})

When to suppress warnings

The ASP0028 diagnostic has Information level severity. Suppress this warning if your intention is to disable IPv6 usage completely on the server, although doing so risks the performance problems mentioned in this article.

IPv6 can be disabled either system-wide, or for .NET only via the AppCtx switch or environment variable