← Foundations path
.NETFoundations 30 / 37Asked by interviewers

Refresh-token rotation and reuse detection

“What is refresh-token rotation, and how does reuse detection catch a stolen token?”

Every refresh token works once. If a used one comes back, someone copied it, so the whole family is cut.

See it

1/7After sign-in, the portal holds refresh token R1. The store keeps R1's hash, in family F.

In plain words

Access tokens are short: about 15 minutes. When one expires, the app sends its refresh token to the API's refresh endpoint and gets a new access token, so the user doesn't sign in again. A refresh token lives much longer (days), so stealing one is worth a lot. It is also opaque: a random string that means nothing by itself, only a key to a database row. That is why it can be cancelled and an access token can't: the API never looks an access token up, it only checks its signature (), so nothing on the server can cancel one before it expires. Below, R1 and R2 are just names for the first and second refresh token. Two rules make theft short-lived. Rotation: each refresh token works once. When the app uses it, the server marks it used and hands back a new refresh token with the new access token. Reuse detection: all the refresh tokens that grew from one sign-in form a family. If a token already marked used is sent again, two parties hold copies: the real app and a thief. The server can't tell which is which, so it revokes the whole family. Both get 401 (the HTTP status for "not signed in") and must sign in again, and the thief's copy is dead. The server stores each refresh token as a hash (a one-way fingerprint), never the token itself, so a database leak doesn't hand out live tokens.

Say it in the interview

In the lending API

The core of a refresh endpoint's service, cut down to the rotation rules. Tap Next line to walk the thief's request.

Lending.sln · where this file sits
  • src/Auth/
Diagram · step 4Earlier, malware copied R1 from the user's laptop. Now the thief tries it.
RefreshService.cs · line 1 · 1 of 12: an async method that returns the new tokens, or null, which means no.

What they ask next

Interviewers check that you can explain every line. Tap a question to open it.

Traps

Read more: OAuth 2.0 Security Best Current Practice (RFC 9700), refresh token protection · Microsoft identity platform: refresh tokens