In ASP.NET Core's AddJwtBearer, when do you configure Authority and Audience, and when TokenValidationParameters.IssuerSigningKey instead — and what must each setup supply?
answer
- who publishes the keys?
- Authority + .well-known/openid-configuration
- RequireHttpsMetadata defaults to true
- no discovery means you set ValidIssuer
- IDX10204 and IDX10208
basics
~20 sUse Authority and Audience when an OpenID Connect provider publishes metadata: the handler discovers the issuer and signing keys. Use IssuerSigningKey only when you hold the key yourself; then you must also set ValidIssuer and ValidAudience, because nothing discovers them.
solid answer
~40 s`Authority` is for tokens from a provider that publishes an OpenID Connect discovery document. The handler builds `MetadataAddress` from it, fetches the issuer and signing keys through a `ConfigurationManager`, caches and refreshes them; `RequireHttpsMetadata` (default `true`) makes an `http://` authority throw an `InvalidOperationException`. `Audience` is copied into `TokenValidationParameters.ValidAudience`. `IssuerSigningKey` is for keys you hold, typically a `SymmetricSecurityKey` for tokens your own service mints. With no metadata, nothing fills in the issuer, so `ValidateIssuer` (default `true`) fails with IDX10204 unless you set `ValidIssuer`/`ValidIssuers`, and the audience check fails with IDX10208 unless you set `ValidAudience` or `Audience`. Keys in code are also never rotated for you, while discovered keys follow the provider's rollover.
code
csharp · 12 lines// Tokens minted by the bank's own internal service, signed with a shared key
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
var key = Convert.FromBase64String(builder.Configuration["InternalTokens:Key"]!);
options.TokenValidationParameters = new TokenValidationParameters
{
IssuerSigningKey = new SymmetricSecurityKey(key),
ValidIssuer = "https://tokens.internal.example.com",
ValidAudience = "statements-service"
};
});go deeper
Remember the split: Authority for a provider that publishes metadata, IssuerSigningKey when you hold the key, and both setups still need an expected audience.
Explain how Authority becomes a metadata address and cached keys, why a static key needs ValidIssuer and ValidAudience, and what IDX10204 and IDX10208 mean.
Argue against pinning a provider's key in code because of rollover, against disabling issuer or audience checks, and against mixing both sources without meaning to.
Weigh where token trust is configured across services — discovery everywhere versus shared keys — against key rotation, blast radius and who can mint tokens.
## Two ways to tell the handler what to trust Every JWT the API accepts must pass three trust checks: it was **signed by a key** the API trusts, it names the **issuer** (`iss`) the API expects, and it is addressed to **this API** (`aud`). `TokenValidationParameters` holds those expectations. The question is where their values come from. | | Authority (discovery) | IssuerSigningKey (static) | |---|---|---| | Source of signing keys | provider's published keys, downloaded | the key object you put in code or config | | Source of the valid issuer | `issuer` in the discovery document | `ValidIssuer` / `ValidIssuers` you set | | Source of the valid audience | `Audience` or `ValidAudience` | `ValidAudience` / `ValidAudiences` you set | | Key rotation | follows the provider's rollover | you redeploy with the new key | | Typical use | external or corporate identity provider | tokens your own service mints; local testing | ## Authority: let the provider describe itself When `Authority` is set and `MetadataAddress` is not, `JwtBearerPostConfigureOptions` builds the metadata address as the authority plus `.well-known/openid-configuration` and creates a `ConfigurationManager<OpenIdConnectConfiguration>` around it. Three details matter: - **HTTPS is enforced.** `RequireHttpsMetadata` defaults to `true`; an `http://` authority makes options post-configuration throw an `InvalidOperationException` that names the setting. Turn it off only for a local provider in development. - **The fetch is lazy and cached.** Metadata is downloaded when the first token is validated, then reused; a token signed with an unknown key triggers a refresh because `RefreshOnIssuerKeyNotFound` defaults to `true`. - **Non-standard providers** that publish their document elsewhere are handled by setting `MetadataAddress` directly. `Audience` is a convenience: if `TokenValidationParameters.ValidAudience` is empty, the post-configure step copies `Audience` into it. For several accepted audiences, set `ValidAudiences`. ## IssuerSigningKey: you are the key holder Setting `TokenValidationParameters.IssuerSigningKey` (or `IssuerSigningKeys`) gives the handler keys directly, for example a `SymmetricSecurityKey` built from a secret shared with the service that issues the tokens. With no `Authority`, there is no discovery document, so: 1. `ValidateIssuer` is still `true` by default. Without `ValidIssuer` or `ValidIssuers`, every token fails with **IDX10204** ("Unable to validate issuer. validationParameters.ValidIssuer is null or whitespace AND validationParameters.ValidIssuers is null or empty"). 2. `ValidateAudience` is still `true`. Without an audience, validation fails with **IDX10208**. 3. Rotation is manual: a new key means new configuration on every instance. Turning `ValidateIssuer` or `ValidateAudience` off to make those errors disappear removes a real check; supply the values instead. ## A common misreading: ValidateIssuerSigningKey `ValidateIssuerSigningKey` defaults to `false`, and people read that as "signatures are not checked". They are: a signed token's signature is always verified against the trusted keys, and `RequireSignedTokens` (default `true`) rejects unsigned tokens outright. `ValidateIssuerSigningKey` adds a check **on the key itself**, such as an X.509 certificate's validity dates, which matters when a key could come from the token. ## For the mobile banking API The banking API validates tokens issued by an external identity provider that rotates its signing keys, so it uses `Authority` plus `Audience` and stores no key at all. A static `IssuerSigningKey` would freeze today's key into the deployment and break on the provider's next rollover. A symmetric key shared with an external provider would also let anyone holding that secret mint banking tokens, which is why external providers sign with asymmetric keys they publish. ## Configuration instead of code `AddJwtBearer` also binds the `Authentication:Schemes:<scheme>` configuration section (`Authority`, `ValidIssuer`, `ValidAudiences`, `SigningKeys` and more). When that section exists, the handler builds a fresh `TokenValidationParameters` from it with `ValidateIssuerSigningKey = true`, which is how `dotnet user-jwts` wires up local testing without code.
- What happens if you set both Authority and IssuerSigningKey?Both sources are used: the handler keeps the keys from `TokenValidationParameters` and also hands the token handler the discovered configuration, so a token signed by either is normally accepted (the IDX10511 failure message lists the keys tried from each). That is rarely intended; it widens what the API trusts and hides which source a token really matched.
- Does replacing TokenValidationParameters with a new object lose the Audience you set?No. The post-configure step runs after your configuration and copies `Audience` into `ValidAudience` whenever `ValidAudience` is empty, so a fresh `TokenValidationParameters` still picks up `Audience`. Its other properties start from their own defaults, such as `ClockSkew` of five minutes and `ValidateIssuer = true`.
- ValidateIssuerSigningKey = false means token signatures are not checked.
- With IssuerSigningKey set, the issuer is taken from the key, so ValidIssuer is unnecessary.
- Set RequireHttpsMetadata = false in production when the Authority is behind a proxy.
- Copy the identity provider's current public key into IssuerSigningKey to avoid the metadata call.
- Disable ValidateIssuer and ValidateAudience to fix IDX10204 and IDX10208.