Samples
Six samples in samples/.
Each is the same shape — a client signing requests to an API that verifies
them — differing in one thing, so the diff between two of them is the
feature.
| Sample | Changes | Api | Client |
|---|---|---|---|
| Simple authentication | The baseline: AddHmac on the API, AddHmacHttpMessageHandler on the client. Start here. | 5100 | 5101 |
| Json configuration | Policies bound from an IConfigurationSection — see configuration binding. | 5110 | 5111 |
| Authorization policies | [Authorize] with RequireHmacAuthentication — see authorization. | 5120 | 5121 |
| Scoped policies | Policies resolved per request — see dynamic policies. | 5130 | 5131 |
| JavaScript client | The client signs with the npm package instead of .NET — see signing requests. | 5140 | — |
| Node API | The API verifies in Node, called by a Node client and a .NET client — see verifying requests. | 5200 | — |
Running one
Each .NET sample has an Api and a client project. Both have to be running:
start the API first, then the client.
cd samples/WebToApiAuthentication
dotnet run --project Api
dotnet run --project Web # in a second terminalThen open the client’s URL. It reports what the API returned for a signed GET
and a signed POST.
The two samples that use the TypeScript client link it from source, so build it once first:
cd client/lib && npm ci && npm run buildInside a sample use npm install, not npm ci — the dependency is a file:
link to client/lib, so there is nothing external to pin and no lockfile is
committed.
Everything listens on plain HTTP on localhost, so no development certificate is needed. HMAC authenticates a request and detects tampering; it does not encrypt one, so a real deployment still belongs behind TLS.
The keys
The keys in the samples are literals committed to the repository. Every sample uses the same key pair on purpose, so that the only difference between any two of them is the feature being shown. They are not examples of key handling — see configuration binding for where a real key belongs.
The samples are built on every pull request, and the two Node ones are run end
to end, by the Sample Builds job in pr.yml. A sample that stops compiling,
stops installing or stops round-tripping fails the build.