How to get IOptions in ConfigureServices method or pass IOptions into extension method?

Viewed 9938

I'm developing asp .net core web api 2.1 app.

I add JWT authentication service as an extension method in static class:

public static class AuthenticationMiddleware
{
    public static IServiceCollection AddJwtAuthentication(this IServiceCollection services, string issuer, string key)
    {
        services
            .AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    // validate the server that created that token
                    ValidateIssuer = true,
                    // ensure that the recipient of the token is authorized to receive it
                    ValidateAudience = true,
                    // check that the token is not expired and that the signing key of the issuer is valid
                    ValidateLifetime = true,
                    // verify that the key used to sign the incoming token is part of a list of trusted keys
                    ValidateIssuerSigningKey = true,
                    ValidIssuer = issuer,
                    ValidAudience = issuer,
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key))
                };
            });

        return services;
    }
}

which I use in ConfigureServices method of Startup class like this:

public void ConfigureServices(IServiceCollection services)
{
    // adding some services omitted here

    services.AddJwtAuthentication(Configuration["Jwt:Issuer"], Configuration["Jwt:Key"]);

    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}

Now, I have a requirement to use IOptions pattern to get JWT authentication data from appsettings.json

How can I get IOptions in ConfigureServices method to pass issuer and key into extension method? Or how to pass IOptions to extension method?

3 Answers

For binding data from appsettings.json to Model, you could follow steps below:

  1. Appsettings.json content

    {
    "Logging": {
     "IncludeScopes": false,
     "LogLevel": {
        "Default": "Warning"
           }
     },      
     "JWT": {
          "Issuer": "I",
          "Key": "K"
        }
     }
    
  2. JWT Options

    public class JwtOptions
    {
        public string Issuer { get; set; }
        public string Key { get; set; }
     }
    
  3. Startup.cs

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<JwtOptions>(Configuration.GetSection("JWT"));
        var serviceProvider = services.BuildServiceProvider();
        var opt = serviceProvider.GetRequiredService<IOptions<JwtOptions>>().Value;
        services.AddJwtAuthentication(opt.Issuer, opt.Key);
        services.AddMvc();
    }
    
  4. One more option to pass JwtOptions directly.

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<JwtOptions>(Configuration.GetSection("JWT"));
        var serviceProvider = services.BuildServiceProvider();
        var opt = serviceProvider.GetRequiredService<IOptions<JwtOptions>>().Value;
        services.AddJwtAuthentication(opt);
    
        services.AddMvc();
    }
    
  5. Change the extension method.

    public static IServiceCollection AddJwtAuthentication(this IServiceCollection services, JwtOptions opt)
    

One other option is to bind the configurations to a class with the Bind() extension. (IMO this a more clean solution then the IOptions)

public class JwtKeys
{
    public string Issuer { get; set; }
    public string Key { get; set; }
}

public void ConfigureServices(IServiceCollection services)
{
    var jwtKeys = new JwtKeys();
    Configuration.GetSection("JWT").Bind(JwtKeys);

    services.AddJwtAuthentication(jwtKeys);
}

public static IServiceCollection AddJwtAuthentication(this IServiceCollection services, JwtKeys jwtKeys)
{....}

Then if you need the JwtKeys settings some other place in the solution, just register the class on the collection and inject it where needed

services.AddSingleton(jwtKeys);

You can add your options to DI container in Startup class like this:

public class JwtOptions
{
    public string Issuer { get; set; }
    public string Key { get; set; }

}

public void ConfigureService(IServiceCollection services)
{
    services.AddOptions();
    services.Configure<JwtOptions>(Configuration.GetSection("Jwt"));
}

Now you can use this options, in a configure stage, or in an extension method:

public void Configure(IApplicationBuilder app)
{
    var options = app.ApplicationServices.GetService<IOptions<JwtOptions>();
    // write your own code
}
Related