optional-token-auth
Skill1 starsChanged 6 days agoMITView on GitHub
How optional JWT bearer authentication works - left out unless the solution was created with --include-auth, every endpoint asks for authorization, the few that stay anonymous, and how to add it later. Use when protecting a new endpoint, making one public, adding authentication to a solution without it, or changing authentication settings.
In this repository40 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- add-a-use-caseSkill
- api-conventionsSkill
- branches-and-pull-requestsSkill
- cancellationSkill
- ci-for-your-solutionSkill
- ci-pipelineSkill
- clean-architecture-layersSkill
- configuration-and-environmentsSkill
- coverageSkill
- csharp-house-styleSkill
- data-access-ef-coreSkill
- database-migrationsSkill
- docker-and-composeSkill
- docs-and-voiceSkill
- errors-and-exceptionsSkill
- expose-a-use-caseSkill
- family-syncSkill
- health-checksSkill
- integration-testsSkill
- json-serializationSkill
- mapping-with-mapperlySkill
- mcp-inspectorSkill
- mediatorSkill
- packages-and-dependenciesSkill
- pagingSkill
- presentation-integration-testsSkill
- releasesSkill
- sample-dataSkill
- security-findingsSkill
- serilogSkill
- telemetrySkill
- template-authoringSkill
- test-designSkill
- time-providerSkill
- unit-testsSkill
- validationSkill
- writing-a-skillSkill
- xml-docs-and-openapiSkill
The file
We split this skill into the rules it gives an agent
6 rules. About 0 are common, 3 are distinctive, and 3 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈3
Distinctive · estimate
This author's own choices. Worth a look.
3
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 21
Only make something anonymous if it has to work without a token: health and the API reference.
- Distinctive: line 22
When authentication is on, the template tells clients how to get a token: a bearer scheme in the OpenAPI document where a template has one, …
- Distinctive: line 23
Test both modes: one test with no authority (anonymous works), one with an authority and no token (401).
- Very specific: line 31
Check Enabled inside an endpoint.
- Very specific: line 32
Put a real authority, client id or secret in appsettings.json.
- Very specific: line 33
Use [AllowAnonymous] to get a test to pass.
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Optional token authentication
The rule
Authentication is a choice made when the solution is created. With --include-auth, --authority and usually --audience, every request needs a valid JWT bearer token from that authority (an OAuth access token in the MCP server), and those options fill in Authentication:Authority and Authentication:Audience. Without --include-auth there's no authentication code at all: no settings, no package, no policy and nothing asking for authorization. If AuthenticationSettings isn't in the solution, it was created without --include-auth; see "Adding it later".
With --include-auth, endpoints always ask for authorization. An empty Authentication:Authority still runs everything open, because the default policy then lets every request through, which is how the tests run unless they set an authority. Only health checks and the API reference (where a template has them) are anonymous. The version endpoint is protected like everything else; telemetry carries service.version for anyone who needs to know what's deployed. The Lambda template has no token authentication; API Gateway or the caller's IAM role does that job.
Settings are read through AuthenticationSettings.FromConfiguration (Enabled is true when the authority isn't empty).
Steps
- In a solution with authentication, a new endpoint or group gets
.RequireAuthorization()(or the presentation layer's equivalent). That's the default; don't skip it. Without it there's nothing to call. - Only make something anonymous if it has to work without a token: health and the API reference. Say why in a comment.
- When authentication is on, the template tells clients how to get a token: a bearer scheme in the OpenAPI document where a template has one, and the protected resource metadata in MCP.
- Test both modes: one test with no authority (anonymous works), one with an authority and no token (401).
Adding it later
A solution created without --include-auth can have it added by hand. The quickest way to see every line is to create a second solution with the same name and --include-auth --authority https://login.example.com, then diff the two. The difference is the authentication package reference in the Presentation project, the Authentication folder and its settings section in appsettings.json, the authentication regions in startup, the anonymous health checks, the call that asks every endpoint or group for authorization, the OpenAPI or MCP metadata that tells clients how to get a token, and the authentication tests. Copy all of it, including the tests, so coverage stays at 95%.
Don't
- Check
Enabledinside an endpoint. The policy setup in startup handles both modes. - Put a real authority, client id or secret in
appsettings.json. - Use
[AllowAnonymous]to get a test to pass.
Check
dotnet test --filter "FullyQualifiedName~Authentication"Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
Reviews can't be read right now.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

