English
Checkpoint
What must work
powershell
dotnet buildpowershell
dotnet test --solution Chatex.slnxThe files of the folder Auth and Users/UserDtos.cs are final and must match the reference:
powershell
git diff --no-index Source/Chatex.Core/Auth D:\Work\Chatex-Reference\Source\Chatex.Core\AuthDependencyInjection.cs, ProfileValidators.cs and BusinessTestBase.cs grow in the next topics; compare them with the listings of this topic.
Commit:
powershell
git add .powershell
git commit -m "Add registration, sign-in and rotating refresh tokens with tests"Common mistakes
Table 8.2. Common mistakes in the authentication logic
| Symptom | Cause | Fix |
|---|---|---|
OptionsValidationException: … SigningKey … when a test starts | the signing key in the test configuration is shorter than 32 characters | use a longer key; the same rule protects production |
Unable to resolve service for type 'FluentValidation.IValidator…' | the validators are not registered, or they are internal without includeInternalTypes: true | check AddValidatorsFromAssemblyContaining<AuthService>(includeInternalTypes: true) |
the lockout test never reaches locked_out | the lockout options are missing, or AccessFailedAsync is not called | check Configure<IdentityOptions> and the wrong-password branch |
Register_rejects_a_taken_user_name_in_any_case fails | the user name is not lower-cased before CreateAsync | UserName = command.UserName.ToLowerInvariant() |
| the expiry test passes only when it runs slowly, or not at all | the service reads DateTimeOffset.UtcNow instead of the TimeProvider | always use timeProvider.GetUtcNow() |
| the access token test fails on the signature | the handler stamped its own times or another key was used | SetDefaultTimesOnTokenCreation = false; one CreateSigningKey |
Zero tests ran | the test class is not public or the methods lack [Fact] | make the class public sealed, add [Fact] |
Self-check questions
- Why does Chatex use two tokens? Compare their lifetimes, storage and revocation.
- What are the three parts of a JWT? Who can read a JWT, and who can create a valid one?
- Why does the token service set the token times itself instead of letting the handler do it?
- Why is a fast SHA-256 hash enough for refresh tokens but not for passwords?
- Why do "unknown user" and "wrong password" return the same error?
- How does rotation of refresh tokens reveal a stolen token? What does the service do then?
- Why is a locked-out account rejected before the password is checked?
- Why does registration run in a transaction? What happens to the user when issuing the tokens fails?
- Why are revoked refresh tokens kept for a full refresh lifetime before they are deleted?
- What does
ValidateOnStartchange compared with validating options on first use? - Why does each test helper call run in its own scope?
- How does a test check a 30-day expiry without waiting 30 days?
Exercises
- A login history.
LoginAsyncrecords nothing about failed attempts besides Identity's counter. Describe (in words or as an interface) what you would store to show a user "the last 10 sign-ins, with browser and IP", and in which layer each part would live. - A new test. Write
Logout_with_an_unknown_token_succeeds: sign-out with a random string must succeed and must not throw. Which line ofLogoutAsyncmakes it pass? - The lockout ends. Write a test that locks an account, advances the fake clock by 16 minutes and signs in successfully. Does it pass? Identity's lockout uses its own clock source — read how
UserManagergets the time, and explain the result. - Inspect a token. In a test or the playground, print an access token and decode its payload on jwt.io. Find
sub,unique_name,iat,nbfandexp, and compute the lifetime fromiatandexp.