English
Topic 8. Authentication logic: ASP.NET Core Identity, JWT access tokens and rotating refresh tokens
Goal: implement registration, sign-in, token refresh and sign-out as business logic — passwords and lockout with ASP.NET Core Identity, short-lived signed JWT access tokens, long-lived refresh tokens that are stored only as hashes, rotate on every use and expose theft — and set up the test project of the business layer with its first twelve tests.
Where we are
The first real service of Chatex.Core (Fig. 8.1). The HTTP side of authentication — the bearer scheme, the refresh cookie — follows in topic 13; here we write the rules.
Fig. 8.1. The authentication service and its dependencies
- Starting point: topic 7 —
Result,Error, validation andAddCoreexist. - At the end of the topic
IAuthServiceregisters and signs users in, refreshes and revokes sessions, and the new test projectChatex.Core.Testsproves it with 12 tests, including lockout, token theft and expiry with a fake clock.
Steps
- Tokens — access and refresh tokens · JWT settings · creating a signed JWT · random refresh tokens and their hashes
- Commands, results and rules — commands and the auth result · user DTOs · errors that reveal nothing · validators
- The authentication service — registration in one transaction · sign-in with lockout · refresh with rotation and reuse detection · sign-out · registration
- Testing the business layer — the test project · a fake clock ·
BusinessTestBase· the auth tests - Checkpoint — what must work, self-check questions and exercises
Materials
- Course slides, part 3, Backend in C# (slides "Authentication with rotating tokens", "Detecting a stolen refresh token", "Rate limiting and lockout")
Auth/,Users/UserDtos.csTests/Chatex.Core.Tests/, in particularAuthServiceTests.csDocs/HttpApi.md– tokens and error codes