dotnet / dotnet/aspnetcore

Add overloads that take an IConfiguration to configure the options

Open
#54,799 1 comment 0 reactions 0 assignees View on GitHub
api-suggestion area-networking
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

## Background and Motivation

Today, there's no easy way to bind the various options to a configuration section apart from doing it manually using the overload
that takes a `Action` like that:

```cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHsts(options => builder.Configuration.GetSection("Hsts").Bind(options));
```

with the following section in the appsettings.json:

```json
{
"Hsts": {
"MaxAge": "365.00:00:00",
"IncludeSubDomains": true,
"Preload": true
}
}
```

## Proposed API
I propose adding overloads that take a configuration section to facilitate the configuration of options:

```diff
public static class HttpsRedirectionServicesExtensions
{
+ public static IServiceCollection AddHttpsRedirection(this IServiceCollection services, IConfiguration configuration);
}

public static class HstsServicesExtensions
{
+ public static IServiceCollection AddHsts(this IServiceCollection services, IConfiguration configuration);
}

public static class HostFilteringServicesExtensions
{
+ public static IServiceCollection AddHostFiltering(this IServiceCollection services, IConfiguration configuration);
}

public static class RateLimiterServiceCollectionExtensions
{
+ public static IServiceCollection AddRateLimiter(this IServiceCollection services, IConfiguration configuration);
}

public static class RateLimiterOptionsExtensions
{
+ public static RateLimiterOptions AddTokenBucketLimiter(this RateLimiterOptions options, string policyName, IConfiguration configuration);
+ public static RateLimiterOptions AddFixedWindowLimiter(this RateLimiterOptions options, string policyName, IConfiguration configuration);
+ public static RateLimiterOptions AddSlidingWindowLimiter(this RateLimiterOptions options, string policyName, IConfiguration configuration);
+ public static RateLimiterOptions AddConcurrencyLimiter(this RateLimiterOptions options, string policyName, IConfiguration configuration);
}

public static class OutputCacheServiceCollectionExtensions
{
+ public static IServiceCollection AddOutputCache(this IServiceCollection services, IConfiguration configuration);
}

public static class AuthenticationCoreServiceCollectionExtensions
{
+ public static IServiceCollection AddAuthenticationCore(this IServiceCollection services, IConfiguration configuration);
}

public static class AuthenticationServiceCollectionExtensions
{
+ public static AuthenticationBuilder AddAuthentication(this IServiceCollection services, IConfiguration configuration);
}

public class AuthenticationBuilder
{
+ public virtual AuthenticationBuilder AddScheme(string authenticationScheme, string? displayName, IConfiguration configuration);
+ public virtual AuthenticationBuilder AddScheme(string authenticationScheme, IConfiguration configuration);
+ public virtual AuthenticationBuilder AddRemoteScheme(string authenticationScheme, string? displayName, IConfiguration configuration);
+ public virtual AuthenticationBuilder AddPolicyScheme(string authenticationScheme, string? displayName, IConfiguration configuration);
}

public static class OAuthExtensions
{
+ public static AuthenticationBuilder AddOAuth(this AuthenticationBuilder builder, string authenticationScheme, IConfiguration configuration);
+ public static AuthenticationBuilder AddOAuth(this AuthenticationBuilder builder, string authenticationScheme, string displayName, IConfiguration configuration);
+ public static AuthenticationBuilder AddOAuth(this AuthenticationBuilder builder, string authenticationScheme, IConfiguration configuration);
+ public static AuthenticationBuilder AddOAuth(this AuthenticationBuilder builder, string authenticationScheme, string displayName, IConfiguration configuration);
}

public static class OpenIdConnectExtensions
{
+ public static AuthenticationBuilder AddOpenIdConnect(this AuthenticationBuilder builder, IConfiguration configuration);
+ public static AuthenticationBuilder AddOpenIdConnect(this AuthenticationBuilder builder, string authenticationScheme, IConfiguration configuration);
+ public static AuthenticationBuilder AddOpenIdConnect(this AuthenticationBuilder builder, string authenticationScheme, string? displayName, IConfiguration configuration);
}

public static class TwitterExtensions
{
+ public static AuthenticationBuilder AddTwitter(this AuthenticationBuilder builder, IConfiguration configuration);
+ public static AuthenticationBuilder AddTwitter(this AuthenticationBuilder builder, string authenticationScheme, IConfiguration configuration);
+ public static AuthenticationBuilder AddTwitter(this AuthenticationBuilder builder, string authenticationScheme, string displayName, IConfiguration configuration);
}

public static class WsFederationExtensions
{
+ public static AuthenticationBuilder AddWsFederation(this AuthenticationBuilder builder, IConfiguration configuration);
+ public static AuthenticationBuilder AddWsFederation(this AuthenticationBuilder builder, string authenticationScheme, IConfiguration configuration);
+ public static AuthenticationBuilder AddWsFederation(this AuthenticationBuilder builder, string authenticationScheme, string displayName, IConfiguration configuration);
}

public static class JwtBearerExtensions
{
+ public static AuthenticationBuilder AddJwtBearer(this AuthenticationBuilder builder, IConfiguration configuration);
+ public static AuthenticationBuilder AddJwtBearer(this AuthenticationBuilder builder, string authenticationScheme, IConfiguration configuration);
+ public static AuthenticationBuilder AddJwtBearer(this AuthenticationBuilder builder, string authenticationScheme, string? displayName, IConfiguration configuration);
}

public static class FacebookAuthenticationOptionsExtensions
{
+ public static AuthenticationBuilder AddFacebook(this AuthenticationBuilder builder, IConfiguration configuration);
+ public static AuthenticationBuilder AddFacebook(this AuthenticationBuilder builder, string authenticationScheme, IConfiguration configuration);
+ public static AuthenticationBuilder AddFacebook(this AuthenticationBuilder builder, string authenticationScheme, string displayName, IConfiguration configuration);
}

public static class GoogleExtensions
{
+ public static AuthenticationBuilder AddGoogle(this AuthenticationBuilder builder, IConfiguration configuration);
+ public static AuthenticationBuilder AddGoogle(this AuthenticationBuilder builder, string authenticationScheme, IConfiguration configuration);
+ public static AuthenticationBuilder AddGoogle(this AuthenticationBuilder builder, string authenticationScheme, string displayName, IConfiguration configuration);
}

public static class CookieExtensions
{
+ public static AuthenticationBuilder AddCookie(this AuthenticationBuilder builder, IConfiguration configuration);
+ public static AuthenticationBuilder AddCookie(this AuthenticationBuilder builder, string authenticationScheme, IConfiguration configuration);
+ public static AuthenticationBuilder AddCookie(this AuthenticationBuilder builder, string authenticationScheme, string? displayName, IConfiguration configuration);
}

public static class MicrosoftAccountExtensions
{
+ public static AuthenticationBuilder AddMicrosoftAccount(this AuthenticationBuilder builder, IConfiguration configuration);
+ public static AuthenticationBuilder AddMicrosoftAccount(this AuthenticationBuilder builder, string authenticationScheme, IConfiguration configuration);
+ public static AuthenticationBuilder AddMicrosoftAccount(this AuthenticationBuilder builder, string authenticationScheme, string displayName, IConfiguration configuration);
}

public static class IdentityServiceCollectionUIExtensions
{
+ public static IdentityBuilder AddDefaultIdentity(this IServiceCollection services, IConfiguration configuration);
}

public static class CookiePolicyServiceCollectionExtensions
{
+ public static IServiceCollection AddCookiePolicy(this IServiceCollection services, IConfiguration configuration);
+ public static IServiceCollection AddCookiePolicy(this IServiceCollection services, IConfiguration configuration);
}

public static class RequestLocalizationServiceCollectionExtensions
{
+ public static IServiceCollection AddRequestLocalization(this IServiceCollection services, IConfiguration configuration);
+ public static IServiceCollection AddRequestLocalization(this IServiceCollection services, IConfiguration configuration);
}

public static class ExceptionHandlerServiceCollectionExtensions
{
+ public static IServiceCollection AddExceptionHandler(this IServiceCollection services, IConfiguration configuration);
+ public static IServiceCollection AddExceptionHandler(this IServiceCollection services, IConfiguration configuration); where TService : class
}

public static class HttpLoggingServicesExtensions
{
+ public static IServiceCollection AddHttpLogging(this IServiceCollection services, IConfiguration configuration);
+ public static IServiceCollection AddW3CLogging(this IServiceCollection services, IConfiguration configuration);
}

public static class WebHostBuilderKestrelExtensions
{
+ public static IWebHostBuilder UseKestrel(this IWebHostBuilder hostBuilder, IConfiguration configuration);
+ public static IWebHostBuilder ConfigureKestrel(this IWebHostBuilder hostBuilder, IConfiguration configuration);
}

public static class WebHostBuilderQuicExtensions
{
+ public static IWebHostBuilder UseQuic(this IWebHostBuilder hostBuilder, IConfiguration configuration);
}

public static class WebHostBuilderSocketExtensions
{
+ public static IWebHostBuilder UseSockets(this IWebHostBuilder hostBuilder, IConfiguration configuration);
}

public static class ResponseCachingServicesExtensions
{
+ public static IServiceCollection AddResponseCaching(this IServiceCollection services, IConfiguration configuration);
}

public static class GrpcJsonTranscodingServiceExtensions
{
+ public static IGrpcServerBuilder AddJsonTranscoding(this IGrpcServerBuilder builder, IConfiguration configuration);
}

public static class HttpJsonServiceExtensions
{
+ public static IServiceCollection ConfigureHttpJsonOptions(this IServiceCollection services, IConfiguration configuration);
}

public static class RequestDecompressionServiceExtensions
{
+ public static IServiceCollection AddRequestDecompression(this IServiceCollection services, IConfiguration configuration);
}

public static class ResponseCompressionServicesExtensions
{
+ public static IServiceCollection AddResponseCompression(this IServiceCollection services, IConfiguration configuration);
}

public static class HeaderPropagationServiceCollectionExtensions
{
+ public static IServiceCollection AddHeaderPropagation(this IServiceCollection services, IConfiguration configuration);
}

public static class RoutingServiceCollectionExtensions
{
+ public static IServiceCollection AddRouting(this IServiceCollection services, IConfiguration configuration);
}
```

## Usage Examples

```cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHsts(builder.Configuration.GetSection("Hsts")); // with the same configuration section as described above
```

## Alternative Designs
If the type of the options is known, the user can configure it using a configuration section like that:

```cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.Configure(builder.Configuration.GetSection("Hsts")); // with the same configuration section as described above
```
But:
- It's less discoverable and friendly because the user has to know the type of the options in advance
- Other parameters cannot be configured at the same time. For example, the display name of an authentication scheme cannot be configured (because it is not part of the options itself).

## Risks

Nothing I can see now.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.