English
Checkpoint
What must work
powershell
dotnet buildpowershell
dotnet test --solution Chatex.slnxpowershell
./Run.ps1 apiCommit:
powershell
git add .powershell
git commit -m "Expose the business layer as a Minimal API with JWT authentication and OpenAPI"Common mistakes
Table 12.1. Common mistakes in the HTTP API
| Symptom | Cause | Fix |
|---|---|---|
every request answers 401, even with a token | MapInboundClaims is true, so sub became a long URI and GetUserId finds nothing — or issuer, audience or key differ from the token service | MapInboundClaims = false; configure the bearer options from JwtOptions |
enums arrive as numbers ("type": 1) | the JsonStringEnumConverter is missing | add it in ConfigureHttpJsonOptions |
errors are empty 401/404 responses without a body | AddProblemDetails or UseStatusCodePages is missing | add both |
/scalar answers 404 | the API runs in another environment | run with the http launch profile (./Run.ps1 api) |
OptionsValidationException about Storage or Jwt at start | appsettings.Development.json lacks the sections | compare with the listing |
| the refresh cookie never comes back in tests | the test client uses http://localhost | use https://localhost as the base address |
WebApplicationFactory<Program> does not compile | Program is not visible | add public partial class Program; at the end of Program.cs |
Self-check questions
- How does ASP.NET Core know where each parameter of a Minimal API endpoint comes from?
- What does a Problem Details response contain? What does Chatex add, and why?
- What does the return type
Results<Ok<T>, ProblemHttpResult>give compared withIResult? - Why is the refresh token in a cookie and the access token not? Explain each of the four cookie attributes.
- Why are all endpoints under
/apiprotected by default? - What do
MapInboundClaims = false,ValidAlgorithmsandClockSkewchange in the token validation? - In which order must the middleware run, and why must authentication come before authorization?
- Why are OpenAPI and Scalar enabled only in Development?
- What does
WebApplicationFactorystart, and what does it replace? - Why do the API tests talk to
https://localhost?
Exercises
- A version endpoint. Map
GET /api/version(anonymous) that returns{ "version": "…" }from the assembly version. Add a test. - A new filter. Add an optional
?type=groupquery parameter toGET /api/chatsthat filters the page by chat type. Which layers change? Is it better done in the endpoint, the service or the repository? - Read the OpenAPI document. Open
http://localhost:5214/openapi/v1.jsonand find the operationCreateChat: which responses are declared, and where did the API learn about them?