aspnet / Configuration

[Archived] Interfaces and providers for accessing configuration files. Project moved to https://github.com/aspnet/Extensions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

IPAddress configuration (or how to configure ForwardedHeadersOptions via appsettings)

drauch opened this issue · comments

We want to configure Microsoft.AspNetCore.Builder.ForwardedHeadersOptions via our appsettings.json file. While "normal" (i.e., string and boolean) properties work just fine this Options class has two "special" properties:

public IList<IPAddress> KnownProxies { get; }
public IList<IPNetwork> KnownNetworks { get; }

which both return a pre-filled (non-read-only) list which can be extended by the user.

How to add entries to those lists using appsettings-based configuration? The rest of the configuration object loads just fine, but a simple:

"KnownProxies": [
    "127.0.0.1"
  ]

does not work. However, we don't know whether it doesn't work because this is the wrong syntax for such list properties or because the correct ctor for IPAddress can't be found.

The binder will try to use TypeConverters to convert from string to the property Type, but it won't look for constructors, so if you register TypeConverters for those types it should work.

So, that these two are read-only properties is no problem for the infrastructure? It just needs a type converter from string to IPAddress and string to IPNetwork and everything's fine?

Yup, tried it, works. Thank you.

@drauch How did you "register TypeConverters" for the IPNetwork or KnownProxies types?