skip to content

In an Aspire AppHost, how does AddParameter with secret: true get its value at run time, and what does marking it secret actually change?

level: seniorimportance: should knowfreq 38%

answer

  1. a resource whose value lives in configuration
  2. the Parameters:{name} key
  3. environment variable Parameters__{name}
  4. dashboard prompts for unresolved parameters
  5. secret is a hint, not encryption

basics

~20 s

AddParameter creates a parameter resource whose value comes from the AppHost configuration key Parameters:{name}, from user secrets, appsettings or environment variables, or else from a dashboard prompt. secret: true masks it and makes publishing treat it as a secret; it encrypts nothing.

solid answer

~40 s

`builder.AddParameter("payment-key", secret: true)` creates a `ParameterResource` and returns its builder. Its value is read from the AppHost's configuration under `Parameters:payment-key`, so it can come from `appsettings.json`, the AppHost's user secrets or an environment variable such as `Parameters__payment_key`. If nothing supplies it, the dashboard shows **Unresolved parameters** and prompts for the value, with an option to save it to user secrets; code that resolves a parameter with no value and no default gets a `MissingParameterValueException`. You pass it on with `WithEnvironment("PAYMENT_API_KEY", paymentKey)` or as an integration's `password:` argument. `secret: true` is a **classification**: the value is masked in the dashboard and treated as a secret at publish time, but user secrets are still plain JSON on disk. Since 13.2, `aspire secret set` writes those values without the .NET CLI.

code

csharp · 16 lines
csharp
var builder = DistributedApplication.CreateBuilder(args);

var paymentKey = builder.AddParameter("payment-key", secret: true)
    .WithDescription("API key for the payment gateway sandbox.");

var dbPassword = builder.AddParameter("postgres-password", secret: true);

var ordersDb = builder.AddPostgres("postgres", password: dbPassword)
    .AddDatabase("ordersdb");

builder.AddProject<Projects.PizzaShop_Api>("pizza-api")
    .WithEnvironment("PAYMENT_API_KEY", paymentKey)
    .WithReference(ordersDb)
    .WaitFor(ordersDb);

builder.Build().Run();

go deeper

for a junior

Recall that AddParameter reads Parameters:{name} from the AppHost's configuration and that user secrets are the usual local home for secret values.

for a middle

Explain the configuration sources and their order, the dashboard prompt for missing values, and how WithEnvironment or a password argument passes the value on.

for a senior

Show that you treat secret: true as classification rather than protection, keep secrets out of appsettings and publishValueAsDefault, and give CI its values through Parameters__ variables.

for a principal

Discuss how parameter names become a contract between developers, CI and each deployment environment, and who owns rotating the values behind them.

## Parameters are resources An Aspire AppHost models external values the same way it models services: as resources. `AddParameter` returns an `IResourceBuilder<ParameterResource>` that appears in the dashboard with the `Parameter` type and can be referenced by other resources. For the pizza-ordering system, the payment gateway's API key is the classic example: the API needs it, it differs per developer and per environment, and it must never be committed. ## Where the value comes from A parameter named `payment-key` is read from the AppHost's `IConfiguration` under the key **`Parameters:payment-key`**. Because that is ordinary .NET configuration, every configured source works: - the AppHost's **user secrets**, the usual place on a developer machine; - `appsettings.json` or `appsettings.Development.json`, for values that are not sensitive; - an **environment variable**, written `Parameters__payment_key` with a single underscore for the dash, which suits CI pipelines; - the command line or any other source added to the builder. The docs describe the effective order as environment variables first, then configuration files and user secrets, then a prompt. `AddParameterFromConfiguration(name, configurationKey)` reads a different key when the value already lives elsewhere, and `AddConnectionString(name)` is the sibling that reads `ConnectionStrings:{name}` for an existing database you do not want Aspire to run. ## When the value is missing Since 9.4 an unresolved parameter no longer stops the run outright. The dashboard shows an **Unresolved parameters** message; **Enter values** opens a form, masks secret input, and offers **Save to user secret** so the next run finds it. `WithDescription(text, enableMarkdown: true)` explains the value in that form, and the experimental `WithCustomInput` callback, diagnostic `ASPIREINTERACTION001`, customizes the input. When a parameter is resolved with no value and no default, the AppHost throws a `MissingParameterValueException` naming the configuration key. ## What `secret: true` changes | Aspect | `secret: false` (default) | `secret: true` | |---|---|---| | Dashboard display | value shown | value masked | | Dashboard prompt input | plain text box | masked input | | Publishing | an ordinary input | a secret input or placeholder | | Storage on the dev machine | whatever source you used | still whatever source you used | The flag is a **hint to tooling**. It does not encrypt the value: user secrets are a JSON file in the user profile, outside the repository but readable by anyone with access to that profile. The overload `AddParameter(name, value, publishValueAsDefault, secret)` gives a default value; `publishValueAsDefault` writes that default into published output and is documented as not meant for sensitive data, so it and `secret` are mutually exclusive. ## Passing the value on A parameter does nothing until a resource consumes it: 1. `WithEnvironment("PAYMENT_API_KEY", paymentKey)` resolves the value when the API starts and sets the variable. 2. Integration methods take parameters directly, for example `AddPostgres("postgres", password: dbPassword)`. 3. Integrations that receive no password create one: `AddPostgres("postgres")` generates a secret parameter named `postgres-password`. The receiving service reads an ordinary environment variable and needs no Aspire package for it. Since 13.2 the CLI manages the AppHost's user secrets directly, so `aspire secret set Parameters:payment-key <value>` replaces `dotnet user-secrets set` for this job. ## Mistakes that leak or lose secrets - **Committing the value.** A real key in `appsettings.json` under `Parameters` works locally and leaks with the next push; keep only non-sensitive defaults there. - **Reading configuration directly.** `builder.Configuration["Parameters:payment-key"]` returns the string, but passing that string with `WithEnvironment` loses the parameter's identity, its masking and its publish-time treatment. - **One shared secret for everyone.** Each developer's AppHost has its own user secrets, so each can use a personal sandbox key; a shared key pasted into a wiki defeats that. - **Forgetting CI.** A pipeline that runs the AppHost has no user secrets and nobody to answer a prompt, so it must provide `Parameters__payment_key` as an environment variable or the parameter ends in the missing-value state. Logs are the other channel: the AppHost records the values of secret parameters so that its backchannel output can redact them, another effect of the flag beyond the dashboard.

  • Why is a parameter better than reading builder.Configuration["PaymentKey"] in AppHost.cs and passing the string?
    A string read in `AppHost.cs` is just a literal: the dashboard cannot prompt for it, mask it or show where it came from, and publishing cannot turn it into a secret input. A `ParameterResource` keeps the value's identity, so the run prompts when it is missing and publishing treats it as a secret.
  • What is the difference between AddParameter and AddConnectionString?
    `AddParameter` reads `Parameters:{name}` and yields a value you pass with `WithEnvironment` or as an argument. `AddConnectionString(name)` reads `ConnectionStrings:{name}` and yields a resource with a connection string, so `WithReference` can hand an existing external database to a service as if Aspire ran it.

  • secret: true encrypts the parameter value at rest on the developer machine.
  • AddParameter values must be stored in the AppHost's appsettings.json.
  • publishValueAsDefault is the right way to ship a secret's default value.
  • A parameter with no configured value silently becomes an empty string.
  • AddConnectionString and AddParameter both read the Parameters section.

Assumes Aspire 13.6; the dashboard prompt for unresolved parameters arrived in 9.4 and the aspire secret commands in 13.2, so older AppHosts need every value configured before the run.