|
| 1 | +`IPv6Any` is preferred to `Any` because `Any` is slower than `IPv6Any`. In some cases, `Any` may not work at all. `Any` has performance problems due to the [underlying System types implementation](https://github.com/dotnet/runtime/issues/82404). |
| 2 | + |
| 3 | +`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. |
| 4 | + |
| 5 | +Currently, when using HTTP/1.x or HTTP/2.0: |
| 6 | + |
| 7 | +* `localhost` resolve to `[::1]`. |
| 8 | +* `[::1]` isn't accepted by the server, which forces a retry using `127.0.0.1`, and the cycle continues. |
| 9 | + |
| 10 | +Using `Any` with the preceding conditions is reported with a diagnostic message: |
| 11 | + |
| 12 | +```csharp |
| 13 | +.UseKestrel().ConfigureKestrel(options => |
| 14 | +{ |
| 15 | + options.Listen(IPAddress.Any, ...); |
| 16 | +}) |
| 17 | +``` |
| 18 | + |
| 19 | +## Rule description |
| 20 | + |
| 21 | +The recommended way to configure Kestrel to listen for incoming connections on all available `IPv6` network interfaces is with `IPv6Any`. |
| 22 | + |
| 23 | +## How to fix violations |
| 24 | + |
| 25 | +For the problematic code, replace `Any` with `IPv6Any`: |
| 26 | + |
| 27 | +```diff |
| 28 | +.UseKestrel().ConfigureKestrel(options => |
| 29 | +{ |
| 30 | +- options.Listen(IPAddress.Any, ...); |
| 31 | ++ options.Listen(IPAddress.IPv6Any, ...); |
| 32 | +}) |
| 33 | +``` |
| 34 | + |
| 35 | +Alternatively, use the `ListenAnyIP` method without specifying any argument: |
| 36 | + |
| 37 | +```diff |
| 38 | +.UseKestrel().ConfigureKestrel(options => |
| 39 | +{ |
| 40 | +- options.Listen(IPAddress.Any, ...); |
| 41 | ++ options.ListenAnyIP(...); |
| 42 | +}) |
| 43 | +``` |
| 44 | + |
| 45 | +## When to suppress warnings |
| 46 | + |
| 47 | +The `ASP0028` diagnostic has a Information level severity. Suppress warnings if your intention is to disable `IPv6` usage completely on the server, although this comes with the risk of the performance problems mentions in this article |
| 48 | + |
| 49 | +`IPv6` can be disabled either system-wide, or for .NET only via the [AppCtx switch or environment variable](https://devblogs.microsoft.com/dotnet/dotnet-6-networking-improvements/#an-option-to-globally-disable-ipv6) |
0 commit comments