With the dotnet user-jwts command, how do you test a JWT-protected ASP.NET Core API locally, and what does the command write into your project?
answer
- a development-only token issuer
- signing key goes to user secrets
- appsettings.Development.json gets ValidIssuer
- Authentication:Schemes:Bearer section
- --role, --scope, --claim name=value
basics
~10 sdotnet user-jwts create issues a signed development token, stores its signing key in the project's user secrets and writes the issuer and audiences under Authentication:Schemes:Bearer in appsettings.Development.json, which AddJwtBearer binds automatically.
solid answer
~40 sRun `dotnet user-jwts create` in the project folder. The tool makes sure the project has a `UserSecretsId`, creates a 32-byte signing key and saves it in user secrets under `Authentication:Schemes:Bearer:SigningKeys`, and writes `ValidIssuer` (`dotnet-user-jwts`) and `ValidAudiences` (the URLs in `launchSettings.json`) to `Authentication:Schemes:Bearer` in `appsettings.Development.json`. It prints a token you send as `Authorization: Bearer`. `AddJwtBearer()` needs no code for this: it binds the `Authentication:Schemes:<scheme>` section and builds `TokenValidationParameters` from it, including the signing key. Options such as `--name`, `--role`, `--scope` and `--claim` shape the claims; `list`, `print`, `remove`, `clear` and `key` manage tokens and the key. Because user secrets and the Development file load only in Development, production keeps its real `Authority`.
code
json · 13 lines{
"Authentication": {
"Schemes": {
"Bearer": {
"ValidAudiences": [
"http://localhost:5080",
"https://localhost:7080"
],
"ValidIssuer": "dotnet-user-jwts"
}
}
}
}go deeper
Recall dotnet user-jwts create, that it prints a token to send as a Bearer header, and the options for name, role and scope.
Explain where the key and settings go — user secrets and appsettings.Development.json under Authentication:Schemes:Bearer — and how AddJwtBearer binds that section.
Point out the scheme-name and environment coupling: settings live under the scheme's key and load only in Development, so production keeps its real Authority.
Decide how a team tests authenticated APIs locally — dev tokens, a local identity provider, or a test authentication handler — and what each misses.
## The problem it solves A Web API protected with `AddJwtBearer` needs a token to call, and during development there may be no identity provider running, or getting a token from it means registering a test client. **`dotnet user-jwts`** is a command-line tool that acts as a tiny local token issuer for one project, wired so the API trusts its tokens only in the Development environment. ## What `dotnet user-jwts create` does Run it in the folder that holds the `.csproj` (or point at one with `-p|--project`): 1. **User secrets.** If the project has no `UserSecretsId`, the tool adds one. 2. **Signing key.** It generates 32 random bytes and stores them, Base64-encoded, in the user secrets under `Authentication:Schemes:Bearer:SigningKeys`, tagged with the issuer `dotnet-user-jwts`. The token is signed with HMAC-SHA256. 3. **Validation settings.** It writes `ValidIssuer` and `ValidAudiences` under `Authentication:Schemes:Bearer` in **`appsettings.Development.json`** (or the file named by `--appsettings-file`). The audiences default to the application URLs in `launchSettings.json`. 4. **The token.** It records the token in a `user-jwts.json` file next to the secrets and prints it. | Option | Effect on the token | |---|---| | `-n` / `--name` | the `sub` and user name; defaults to your OS user name | | `--role` | adds a role claim (repeatable) | | `--scope` | adds a `scope` claim (repeatable) | | `--claim name=value` | adds any claim | | `--audience`, `--issuer`, `--scheme` | override the defaults | | `--not-before`, `--expires-on`, `--valid-for` | lifetime; by default it expires three months after `--not-before` | Other subcommands: `list` shows issued tokens, `print <id>` (with `--show-all`) decodes one, `remove` and `clear` delete them, and `key` shows or, with `--reset`, regenerates the signing key. ## Why the API trusts it without code `AddJwtBearer` registers a configure step that reads the `Authentication:Schemes:<scheme name>` configuration section. When that section has content, the handler takes `Authority`, `ValidIssuer`, `ValidAudiences` and the rest from it and builds a new `TokenValidationParameters`, including `IssuerSigningKeys` from the matching `SigningKeys` entry. So a bare `AddJwtBearer()` in `Program.cs` accepts the tool's tokens. The protection against shipping this to production is the environment: `WebApplication.CreateBuilder` loads user secrets only when the environment is Development, and `appsettings.Development.json` is loaded only there too. In production the same section is absent or holds the real provider's `Authority`. ## A typical session for the banking API ```bash dotnet user-jwts create --name teller01 --role teller --scope "accounts:read" dotnet user-jwts list dotnet user-jwts print $TOKEN_ID --show-all curl -i -H "Authorization: Bearer $TOKEN" https://localhost:7080/accounts/me ``` Things to know: - The tool's settings sit under the scheme name, `Bearer` by default. If the API registers `AddJwtBearer("Partner")`, pass `--scheme Partner` or the configuration lands under the wrong key. - Tokens are tied to the project's signing key; `key --reset` invalidates every token issued before it. - It mints development tokens only. Real issuance, key storage and rotation belong to the identity provider. ## What it does not cover - **Asymmetric keys and discovery.** The tool signs with a shared HMAC key read from configuration, so the `Authority`, metadata download and key-rollover path the production API uses is never exercised locally. - **Automated tests.** Integration tests often swap in a test authentication handler or mint tokens in the test itself rather than shelling out to the tool. - **Other environments.** A staging environment that is not named Development loads neither user secrets nor `appsettings.Development.json`, so its tokens must come from a real provider.
- Why does [Authorize(Roles = "teller")] accept a token created with --role teller?The tool adds a role claim, which reaches the token as `role`; JwtBearer's default inbound claim mapping turns it back into `ClaimTypes.Role`, the identity's default role claim type, so the role check matches. With `MapInboundClaims = false` you would also have to set `RoleClaimType` to `role`.
- The API registers AddJwtBearer("Partner"), and tokens from dotnet user-jwts create fail. Why?The tool writes its issuer, audiences and signing key under `Authentication:Schemes:Bearer` by default, while the handler reads `Authentication:Schemes:Partner`. Create the token with `--scheme Partner` so the settings land in the section that scheme binds.
- dotnet user-jwts stores the signing key in appsettings.Development.json.
- You must add a TokenValidationParameters block in Program.cs for user-jwts tokens to validate.
- dotnet user-jwts tokens also work in production because the key is in the project.
- dotnet user-jwts disables signature validation in Development.
- --scope teller gives the user the teller role.