Skip to content

Better way to get the Json Web Key in AddJwtBearer #21491

@Vaccano

Description

@Vaccano

Is your feature request related to a problem? Please describe.

I am trying to set the IssuerSigningKey (aka Json Web Key) for the TokenValidationParameters that is provided in AddJwtBearer options class (JwtBearerOptions). I get this value from a web page provided by my IDP.

It is a common practice to put this (and other settings for the IDP, like issuer) on a page hosted by the IDP under .well-known/openid-configuration.

When setting up the AddJwtBearer lambda, there is no way to get access to any classes/objects that are setup to do this. (I wrote the class to get the value and cache it, but I can't get access to it.)

This is because you have two access points in relation to AddJwtBearer:
1. ConfigureServices:
When you define AddJwtBearer in ConfigureServices cannot get any dependency injected services without calling BuildServiceProvider. (Which, if you do, causes two of your singletons to be created for your application.) The other option is to new an object outside the Dependency Injection system. (Which gives you an extra instance as well.)

Either way, if you do this, you can access the value as an implicitly captured closure inside the AddJwtBearer. While ugly, this is the only way that can actually work.

2. When the AddJwtBearer Lambda is Invoked:
At this point there is no way to get access to any dependency injection objects. Not because the graph is not created, but because you don't have an instance of the ServiceProvider to call GetService on.

Describe the solution you'd like

Allow the JwtBearerOptions to call GetRequiredService. Or have an instance of ServiceProvider on it. Or some other way to access the Dependency Injection system in the lambda for AddJwtBearer.

That way you can get an object that will make the call to the IDP page and download the IssuerSigningKey (also known as the Json Web Key).

Additional context

As a side note, it would be nice if the call to AddJwtBearer did not happen during the first call. (That way the first call does not have to take the hit for calling out to the IDP page.) Some point in time around when Configure is called would be nice (after the ServiceProvider is built, but before calls are coming in.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-authIncludes: Authn, Authz, OAuth, OIDC, Bearer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions