skip to content

With AddAzureKeyVault in ASP.NET Core, how does a secret named Maps--ApiKey become a configuration key, and how do a custom KeyVaultSecretManager and ReloadInterval change what loads?

level: seniorimportance: should knowfreq 32%

answer

  1. no colon allowed in secret names
  2. -- becomes :, --0-- for arrays
  3. GetKey and Load(SecretProperties)
  4. disabled skipped, expired loaded
  5. ReloadInterval null: loaded once

basics

~20 s

KeyVaultSecretManager.GetKey replaces -- with :, so Maps--ApiKey becomes Maps:ApiKey. A subclass can override Load to choose which secrets are fetched and GetKey to rename them; ReloadInterval, null by default, makes the provider poll for changes.

solid answer

~40 s

Key Vault secret names allow only letters, digits and dashes, so the provider's default `KeyVaultSecretManager.GetKey` replaces every `--` with `ConfigurationPath.KeyDelimiter`: `Maps--ApiKey` becomes `Maps:ApiKey` and `Dispatch--Zones--0` becomes array element `Dispatch:Zones:0`. Disabled secrets are never loaded; expired ones are, because the default `Load(SecretProperties)` returns `true`. Subclassing the manager lets you override `Load` to filter secrets before their values are fetched (by expiry or by a version prefix) and `GetKey` to strip that prefix. `AzureKeyVaultConfigurationOptions.ReloadInterval` is `null` by default, meaning load once; a positive `TimeSpan` starts a poll that re-lists the vault, refetches secrets whose update time changed, and fires the reload token. Errors during a poll are swallowed, so the last loaded values stay.

code

csharp · 14 lines
csharp
public sealed class UnexpiredSecretManager : KeyVaultSecretManager
{
    public override bool Load(SecretProperties properties) =>
        properties.ExpiresOn is null || properties.ExpiresOn > DateTimeOffset.UtcNow;
}

builder.Configuration.AddAzureKeyVault(
    new Uri("https://dispatch-prod.vault.azure.net/"),
    new DefaultAzureCredential(),
    new AzureKeyVaultConfigurationOptions
    {
        Manager = new UnexpiredSecretManager(),
        ReloadInterval = TimeSpan.FromMinutes(10),
    });

go deeper

for a junior

Remember that a vault secret named Maps--ApiKey is read as Maps:ApiKey, because colons are not allowed in secret names.

for a middle

Explain GetKey's -- replacement, array names with numeric segments, and that disabled secrets are skipped while expired ones load.

for a senior

Write a manager that filters in Load and renames in GetKey, set ReloadInterval for rotation, and make sure long-lived clients pick up new values.

for a principal

Choose between per-environment vaults and prefix conventions with access isolation in mind, and plan rotation so old and new keys overlap safely.

## Why the double dash ASP.NET Core configuration separates sections with `:`, but a Key Vault secret name may contain only **alphanumeric characters and dashes**. The provider therefore uses `--` as the section separator in the vault and converts it when loading: | Secret name in the vault | Configuration key | |---|---| | `Maps--ApiKey` | `Maps:ApiKey` | | `Sms--AuthToken` | `Sms:AuthToken` | | `Dispatch--Zones--0` | `Dispatch:Zones:0` (array element 0) | | `Maps-ApiKey` | `Maps-ApiKey` (a single dash is kept) | The conversion lives in `KeyVaultSecretManager.GetKey`, which is `secret.Name.Replace("--", ConfigurationPath.KeyDelimiter)`; every occurrence is replaced, not just the first. ## What the default manager loads The provider asks the manager about each secret it lists: - **`Load(SecretProperties)`** decides whether the value is fetched at all. The default returns `true` for everything. - **Disabled secrets** are skipped by the provider itself, whatever `Load` says. - **Expired secrets** are loaded: an expiry date in the vault does not stop the value reaching configuration. - **`GetData`** turns the fetched secrets into the key dictionary. If two secrets map to the same key, the one updated most recently wins. ## A custom manager Subclass `KeyVaultSecretManager` and override the members you need: ```csharp public sealed class VersionPrefixSecretManager(string prefix) : KeyVaultSecretManager { public override bool Load(SecretProperties properties) => properties.Name.StartsWith(prefix, StringComparison.OrdinalIgnoreCase) && (properties.ExpiresOn is null || properties.ExpiresOn > DateTimeOffset.UtcNow); public override string GetKey(KeyVaultSecret secret) => secret.Name[prefix.Length..].Replace("--", ConfigurationPath.KeyDelimiter); } ``` With prefix `v5-`, the secret `v5-Maps--ApiKey` becomes `Maps:ApiKey`, and secrets for other app versions are never fetched. The provider docs use prefixes for **app versions** and warn against using them to put several apps, or Development and Production, in one vault: separate vaults isolate access far better than a naming convention. Pass the manager through an overload or the options object: ```csharp builder.Configuration.AddAzureKeyVault(vaultUri, new DefaultAzureCredential(), new AzureKeyVaultConfigurationOptions { Manager = new VersionPrefixSecretManager("v5-"), ReloadInterval = TimeSpan.FromMinutes(10), }); ``` ## Reloading `AzureKeyVaultConfigurationOptions.ReloadInterval` controls rotation pickup: 1. **`null` (default):** secrets load once at startup; a rotated value needs a restart. 2. **A positive `TimeSpan`:** after the first load a background loop waits the interval, lists the secrets again, fetches only those whose update time changed, drops removed or disabled ones, and calls `OnReload()` when something changed. 3. **Zero or negative:** rejected with `ArgumentOutOfRangeException` when the provider is created. A failed poll (network error, revoked access) is ignored and the last loaded values stay; the next interval tries again. Polling costs a list call per interval plus a get per changed secret. ## Who sees a rotated value - Reads through `IConfiguration` see it after the reload. - Values copied at startup, such as a maps client built once with the key in its constructor, keep the old key until the process restarts. - During rotation keep the old key valid until every instance has polled or restarted. ## Interview checklist - `--` becomes `:`; arrays use numeric segments. - Disabled skipped, expired loaded by default. - Override `Load` to filter before fetching, `GetKey` to rename. - `ReloadInterval` is `null` by default; polling errors are swallowed.

  • Should one vault hold Development and Production secrets separated by a name prefix?
    No. The provider docs warn against prefixes for separating apps or environments; use one vault per app and environment so each identity can read only its own secrets. Prefixes suit cases such as app versions sharing one vault, where every reader is allowed to see every version.
  • A Maps--ApiKey rotation was picked up by the 10-minute poll, yet the dispatch API still calls the maps service with the old key. Why?
    The poll updated the provider and fired the reload token, so fresh `IConfiguration` reads return the new key. The maps client, however, copied the key when it was constructed at startup and never reads configuration again. Read the key per call, rebuild the client on change, or restart the instances.
  • What happens if the vault is unreachable during a reload poll?
    The polling loop catches the exception and ignores it. The provider keeps the values from the last successful load, and the next interval tries again. The app keeps running, which also means a long outage can go unnoticed unless you watch for it elsewhere.

  • Key Vault turns a single dash, as in Maps-ApiKey, into a colon.
  • ReloadInterval defaults to a few minutes, so rotated secrets appear automatically.
  • The default KeyVaultSecretManager skips expired secrets.
  • A failed reload poll clears every vault value from configuration.
  • Name prefixes are the recommended way to share one vault between Development and Production.