Auth & the edge · No. 16
Cross-service auth without a shared secret: JWKS discovery

When you split a monolith into services, the easy answer is to copy the JWT signing secret into every validator. That is also the answer that turns one leaked key into a system-wide breach. Here is the asymmetric boundary that fixes it.
You have a monolith that mints its own JWTs. One process signs the token, the same process validates it, and the signing key is a symmetric secret (HS256). This is fine. The signer and the validator are the same code, so the shared secret is not really shared with anyone.
Then you extract a module into its own service. Now the Orders service needs to validate a token that the Identity service minted. The obvious move is to copy the HS256 secret into the Orders service config so it can validate. Then into the Catalog service. Then into the gateway. Then into the notification worker.
Stop and look at what just happened. With a symmetric secret, the key that validates a token is the same key that mints one. Every service that can check a token can also forge one. You have taken a single high-value secret and replicated it across every deployment unit, log scrape, and environment variable in the fleet. The blast radius of one leak is now "the entire system can be impersonated."
Why it matters
Symmetric signing couples trust to secret distribution. The more services you have, the more copies of the forge-anything key exist, and the more places it can leak: a misconfigured config map, a debug log that dumped the environment, a developer laptop, a third-party APM that captured an env var. You cannot rotate it without a coordinated flag day across every service at once, because they all share it.
The correct model for distributed token validation is asymmetric: the issuer signs with a private key it never shares, and every other service validates with the matching public key. A public key is public. It cannot mint tokens. Leaking it is a non-event. That is the property you want for a boundary that many services depend on.
The problem is that "just switch to RS256" is not the whole story. The validators still need to get the public key, keep getting it as it rotates, and refuse to be tricked into validating the wrong way. That is what MMCA.Common's JWKS layer is built to handle.
The MMCA answer: publish the public half as a JWKS document
The framework's token service supports two signing algorithms, selected by the concrete
JwtSettings.SigningAlgorithm, which defaults to RS256:
- Monolith mode: HS256 (symmetric HMAC-SHA256). One process signs and validates with a shared Base64 secret. A shared secret inside one process is not actually shared, so this is correct here.
- Microservice mode: RS256 (asymmetric RSA-SHA256). The issuer signs with its RSA private key; other services validate with the matching public key and hold no secret at all.
The join point that makes RS256 work across services is JWKS (JSON Web Key Set). In MMCA.Common,
IJwksProvider (in MMCA.Common.Infrastructure, implemented by RsaJwksProvider) materializes a
JsonWebKeySet from a PEM-encoded RSA public key. Critically, it exports only the public
parameters: the private key never leaves the issuer. The set is built lazily on the first request, and
only a successful result is cached: the Lazy<JsonWebKeySet> is created with
LazyThreadSafetyMode.PublicationOnly, so one transient failure reading the PEM is retried on the next
call rather than cached for the life of the process.
The API layer's JwksEndpointExtensions serves that key set at the standard discovery path,
/.well-known/jwks.json. A downstream service points its JwtBearer handler at that URL and the
ASP.NET Core JWKS machinery fetches the issuer's public keys, validates incoming RS256 tokens against
them, and refreshes the key set on its own schedule. The Identity service holds the one private key;
every validator holds nothing.
// Issuer side (Identity API): RsaJwksProvider exposes ONLY the public key.
// Served at GET /.well-known/jwks.json by JwksEndpointExtensions.
JsonWebKeySet GetJsonWebKeySet(); // public parameters only; private key never leaves
// Validator side (any extracted service): JwtBearer fetches the issuer's JWKS URL
// (through the gateway under the local AppHost, Identity's internal ingress when deployed)
// and validates RS256 tokens against the public keys.
// The validator holds NO secret. A leaked public key cannot mint tokens.
(Source: RsaJwksProvider.cs, IJwksProvider.cs, and the JWKS notes in MMCA.Common/AGENTS.md's
microservices extraction section.)
Discovery, and the graceful empty set
Discovery has two halves, and ADR-004 names both. On the issuer, when JWKS publishing is disabled (the
default for a monolith) or no key is configured, RsaJwksProvider returns an empty key set rather
than throwing. The well-known endpoints are mapped centrally for every host, so the endpoint stays a
valid, pollable URL instead of erroring. A monolith that never turns JWKS on still answers
/.well-known/jwks.json with an empty document, so flipping a service into extracted mode does not
require standing up new endpoints, and only the real issuer advertises a key.
On the validator, the question is which URL to fetch from, and the answer depends on where the host
runs. Under the local Aspire AppHost, WithJwksDiscovery(identity, gateway) sets each validator's
authority to the gateway's HTTPS origin: the extracted REST services listen HTTP/2-only on
cleartext for h2c gRPC (ADR-012), so the default HTTP/1.1 JwtBearer backchannel cannot reach Identity
directly, while the gateway terminates TLS, speaks both protocols, and forwards /.well-known/* on to
Identity. When no gateway is passed, the helper falls back to Identity's own HTTPS endpoint. In the
deployed ADC and Store environments, the bicep templates point the authority straight at Identity's
internal-ingress URL, with no gateway hop. Either way the validator carries one authority value owned
by the host wiring and no key material, and ValidIssuer is deliberately left unpinned: the authority
need not be the token's iss origin (locally it is the gateway's HTTPS endpoint; when deployed it is
Identity's internal-ingress URL while the token's iss is the public gateway origin), so the issuer is
taken from the discovery document.
ADR-004's file name says "dual-fetch", but the framework's own doc comments give that name to a
different flow, so it is worth naming plainly: the shared login flow,
AuthenticationServiceBase<TUser>.LoginAsync in MMCA.Common.Application, does an untracked
read to validate credentials (so the adversarial majority of login traffic, the failed attempts, never
pays EF Core change-tracking cost), then a tracked re-fetch only on success. Refresh tokens live in
their own session rows, so the re-fetch is not about persisting them: it loads the instance the app's
CreateAccessToken hook mints from (apps reach linked aggregates and navigations through it), and the
second lookup turns a race that deleted the account between the two steps into a clean 404. The base's
own doc comment names it the "untracked-then-tracked dual-fetch"; the ADC and
Store Identity modules are thin sealed subclasses that supply the app-specific hooks (the untracked
lookup, the claim set) and re-label it "dual-fetch pattern" in their class doc comments
(AuthenticationService.cs:23 in ADC, :20 in Store). It is shared framework login code, not a
per-app copy, but it is not part of ADR-004's cross-service validation design.
JWT algorithm pinning: the attack you would otherwise ship
Switching to asymmetric keys opens a specific, classic attack, and the framework closes it explicitly.
Once the RSA public key is published (which is the entire point of JWKS), an attacker has it. If a
validator naively trusts the token's own alg header, the attacker can craft a token that claims
alg: HS256 and sign it using the public RSA key bytes as the HMAC secret. A validator that reads
alg from the attacker-controlled header would happily verify it. This is the well-known algorithm-
confusion / key-substitution attack.
MMCA.Common's TokenService pins the algorithm. On the refresh path, GetPrincipalFromExpiredToken
deliberately skips lifetime validation (its job is to read claims out of an already-expired token, with
the relevant analyzer warning suppressed inline and justified) but it re-checks the token's alg
header against the expected algorithm and restricts ValidAlgorithms to the single expected value. An
attacker cannot substitute an HS256-signed token in place of a real RS256 one. The same pin guards
the cross-service path: the JWKS-forwarded bearer handler (AddForwardedJwtBearer, used by extracted
services validating against the issuer's published keys) also restricts ValidAlgorithms to
[RsaSha256] (added in v1.82.0), so a token whose header claims HS256 is rejected on the discovery
path too, not only in-process. The token service is also IDisposable and owns its RSA handles,
releasing the native key handles on disposal.
The rule of thumb: never let the token tell you how to verify it. The validator decides the algorithm; the token only supplies the signature.
When there is no user: the opt-in service token
Everything above covers calls that carry a user. A typed gRPC client registered with
AddTypedGrpcClient<TClient> forwards the inbound user's bearer token through
JwtForwardingClientInterceptor, and the downstream service validates it against the issuer's
published keys like any other request. Some calls have no user to forward: a background processor, a
webhook, an anonymous request that fans out internally. For those, MMCA.Common.Grpc ships a service
token, and it is opt-in per client.
The calling host registers AddServiceTokenClient(configuration), which binds and validates the
Grpc:ServiceToken section on start and registers ServiceTokenProvider as a singleton. That sends
nothing by itself: a client carries the token only after .WithServiceToken() is chained onto its
registration, which adds ServiceTokenClientInterceptor to that client alone. The interceptor attaches
Bearer {serviceToken} only when the ambient request has no user token, and never overwrites an
Authorization entry already on the call, so the user's token always wins.
The provider mints an HS256 token whose sub and svc claims name the calling service, with iss
and aud from settings, a fresh jti, and a five-minute default lifetime (validated to 1 through 60).
One token serves every outgoing call until its last 60 seconds:
// Abridged from ServiceTokenProvider.GetToken (MMCA.Common.Grpc).
var cached = Volatile.Read(ref _cached);
if (IsFresh(cached, now)) { return cached.Value; } // more than 60 s of lifetime left
lock (_gate)
{
cached = _cached; // another caller may have minted
if (IsFresh(cached, now)) { return cached.Value; }
var minted = Mint(now); // HS256, sub and svc = ServiceName
Volatile.Write(ref _cached, minted);
return minted.Value;
}
The receiving service opts in separately with AddServiceTokenAuthentication: a second JwtBearer
scheme that pins ValidAlgorithms to HS256, refuses a token without the svc claim, and is never the
default scheme. It registers two policies, and only an endpoint under one of them accepts the token.
InternalCaller takes an authenticated caller from either scheme, so a user token on the same call is
validated by the user scheme against the issuer's JWKS exactly as described above, and an edge that
admits both kinds of caller tells them apart with IsServiceCaller(). ServiceCaller evaluates the
service scheme alone, so an end-user token is refused there.
Both apps adopt it. In Store, the Sales service sends the token on its Customers client, and Identity
validates it on the Customers edge under InternalCaller. ADC runs three such edges: Engagement sends
its token to Identity's VerifiedUserLookup edge (InternalCaller, reached by the roster import, which
has no user) and to Notification's LiveChannelPush edge (ServiceCaller, reached only by the
live-channel publish drain), and Identity sends its own to Engagement's TicketInvitation edge
(ServiceCaller, for the anonymous sign-in link request).
Trade-offs, honestly
The JWKS layer is the right model, but it is not free, and the rough edges are worth naming.
Two round trips on successful login. The Identity service's login flow (the shared login dual-fetch above, not the JWKS layer itself) pays a tracked re-fetch, a second query on the success path. Failed logins (the adversarial majority) never reach that second query, though an unknown email still pays one real password-verification cost on purpose, so its timing matches a known account's. It is a deliberate trade, not an oversight.
A small race window between the two fetches. A user could be deleted between the untracked validation and the tracked re-fetch. The re-fetch then returns NotFound, which is the correct outcome, so the window is acceptable rather than hidden.
Plain-HTTP metadata discovery is an explicit, auditable opt-out.
AddForwardedJwtBearerresolvesRequireHttpsMetadatain three steps: explicit argument, then theAuthentication:JwtBearer:RequireHttpsMetadataconfig key, thentrueeverywhere except Development, and a resolvedfalseoutside Development logs one startup warning naming the key. The honest residual is that an operator can still set that key tofalse, and both production deployments do: ADC and Store validate against Identity's cleartext internal-ingress URL, so their bicep sets it explicitly with the justification written beside it. That is a reviewable line in a deployment template rather than a default nobody chose.Every dev-only relaxation fails closed on an unknown environment. The three-step resolution above is one instance of a framework-wide rule (ADR-122): a relaxation applies only when the host positively identifies itself as Development, and an unknown, absent, or unparseable environment gets the production posture. The case that rule exists for is easy to miss: design-time tooling, a directly-constructed test context, and a service collection assembled outside a host builder register no
IHostEnvironmentat all, so every gate has to decide what "unknown" means.Three gates take a nullable environment and answer "not Development" for null.
SensitiveDataLoggingGate.IsEnabledlets EF Core render parameter values into logs only when the setting asks for it AND the environment reports Development, so a configuration file copied into a deployed host cannot put tokens and password hashes into the log sink on its own. SMTP transport security reads theSmtp:EnableSslkey first and otherwise enables TLS unless the environment is positively Development, and a deployed host that turns TLS off logs a startup warning. The AI package scans the service collection for a registeredIHostEnvironmentand keeps prompt and completion text out of telemetry when it finds none.The residuals are named in the ADR itself.
ASPNETCORE_ENVIRONMENTis the trust root, so a host that misreports itself as Development gets every relaxation at once: the rule concentrates the risk on one value instead of removing it. The other Development-gated relaxations (the permissive dev CORS policy, the cookieSecureflag, HSTS and the CSP built from the same flag, the pseudo-locale, the design-time database skip) are plainIsDevelopment()checks, not fail-closed gates: the server-side ones read an environment the host always supplies, and the one UI-side check (the culture switcher offering the pseudo-locale) reads a nullable environment and treats null as not Development. One endpoint mapping sits outside the rule altogether: the OpenAPI document is gated on!IsProduction()(OpenApiEndpointExtensions.cs:61) rather than on Development, so a Staging host gets it (it exposes API description, not data). And the inventory is manual: nothing fails a build when a new environment-conditional branch skips the rule.The invariants in this article fail the build. Executable tests run the real registration code and read the produced options back: the forwarded bearer handler's
ValidAlgorithmsmust stay[RsaSha256], theRequireHttpsMetadataresolution above is asserted step by step (including the fail-fast on a cleartext authority with no opt-out), the permissive CORS policy must never support credentials while the credentialed one must never widen to any origin, andRsaJwksProvidermust export only the public RSA parameters even when handed a private-key PEM. Beside them, a sharedAnonymousEndpointTestsBasescans controllers and routable components by reflection and ships five facts: it fails on any[AllowAnonymous]outside an explicit allow-list, on a stale allow-list entry, on an empty scan, on an endpoint that declares neither[Authorize]nor[AllowAnonymous](a stricter gate that is on by default, which a repo not yet ready opts out of explicitly; Common, ADC, Store and Helpdesk all keep it on), and on a stale entry in that second, undecorated allow-list. The framework's own allow-list holds 29 entries: nineteen credential-exchange actions that cannot require a token because issuing one is what they do, or because the caller has no session yet (login, register, refresh, the OAuth exchange, the four provider challenges for Google, GitHub, Apple and OIDC and the provider callback, OIDC home-realm discovery, forgot-password and reset-password, sending and confirming an email-confirmation link, sending and redeeming a sign-in link, beginning and completing a passkey sign-in, and the machine-client token endpoint), seven credential pages (the email-confirmation and sign-in-link landings among them), and three landing and outcome pages that render nothing depending on the caller. The scan matchesControllerBase,RouteAttributeandAllowAnonymousAttributeby full-name reflection, which keeps the shared test package free of ASP.NET references, and the bearer and CORS invariants run the real registration code and read the options back. Two residuals stay honest. Minimal-API endpoints opt out through an.AllowAnonymous()builder call, which is endpoint metadata rather than an attribute, so the JWKS and OIDC discovery endpoints themselves sit outside the scan's reach. And the anonymous-endpoint scan is only as complete as the suites that subclass it: each consuming repo owns its own allow-list. The framework's thesis is that a rule that matters should fail the build, and these rules do.The service token is one shared secret, scoped to the edges that opt in. It is HS256 over a single key that the callers and the receivers both hold, so any holder can mint a token naming any service, and the receiving scheme checks that the
svcclaim is present, not which service it names. What bounds it is the opt-in on both sides: the scheme is never the default, a plain[Authorize]endpoint still authenticates the user scheme alone, and only endpoints underInternalCallerorServiceCalleraccept it. Per-service keys or transport-level service identity are a deliberate non-goal for traffic that stays inside the Container Apps environment; the governance backlog names a service reachable from outside it, or a second team owning a service, as the condition to revisit that.
None of these are reasons to share a symmetric secret for user tokens across services. They are the reasons to wire the asymmetric boundary deliberately, and to keep the one shared key opt-in and narrow.
Apply this even without MMCA
The pattern ports to any stack with a JWT story:
- In a single process, symmetric (HS256) is fine. Do not add asymmetric complexity you do not need. The signer and validator are the same code.
- The moment a second service validates tokens it did not mint, go asymmetric (RS256/ES256). The issuer keeps the private key; everyone else gets the public key. A validator should never hold a key that can also forge.
- Publish the public key as a JWKS document at
/.well-known/jwks.jsonand let the standardJwtBearerdiscovery fetch and refresh it. Give each validator one authority value owned by the deployment wiring (a gateway origin or the issuer's internal address), so a topology change is a config edit rather than a key redistribution. - Pin the algorithm on the validator. Set the allowed algorithms explicitly and re-check the
token's
algagainst your expectation. Never let the token's own header choose the verification method, or you have shipped the algorithm-confusion attack. - Fall back gracefully. Serve an empty key set rather than erroring when JWKS is off, so the same code path works in monolith and extracted modes.
The takeaway: the key that validates a token should never be the key that mints one. Asymmetric signing plus JWKS discovery is how you keep that true across a fleet, and it is the extraction point that makes pulling a module out into its own service a config change rather than an auth rewrite.
What we covered: why a shared symmetric secret turns one leak into a system-wide forgery, how RS256 + a published JWKS document lets extracted services validate against the issuer's public keys with no secret of their own, how discovery (gateway-routed under the local AppHost, direct to Identity's internal ingress when deployed) and the empty-key-set fallback keep the boundary stable, why algorithm pinning closes the key-substitution attack that publishing a public key would otherwise open, how the opt-in service token covers calls with no user while keeping its one shared key narrow, and how ADR-122 makes every dev-only relaxation fail closed when the environment is unknown.
Next in the series: password hashing done right: PBKDF2-SHA512, 600k iterations, and timing-safe comparison, the credential-at-rest half of the same security story.
MMCA.Common is Apache-2.0 licensed and open source. Star the repo, read the 2-minute ADR-004 behind this
pattern, or dotnet add package MMCA.Common.API and try it.
- ⭐ Repo: https://github.com/ivanball/MMCA.Common
- 📄 ADR-004 (cross-service token validation via JWKS / OIDC discovery): ADR-004: Cross-Service Token Validation via JWKS / OIDC Discovery in the docs site.
Tags: .NET, C Sharp, Software Architecture, Microservices, Authentication