InvalidToken: Invalid token. is the error that ResetPasswordAsync
returned in three different situations in the lab: the token was read back from a link and its
first + came back as a space, it belonged to another user, or the user's password had changed
since it was issued. The message is the same for all three, so check them in that order.
For the link case, encode the token with WebEncoders.Base64UrlEncode before it goes
into the URL and decode it when it comes back; that run succeeded. The other two are not bugs
in the code: issue a new token for the right user.
The error
Console output of the lab, one line per case:
A raw token in link, + survived: False
A: Failed : InvalidToken
InvalidToken: Invalid token.
B: Failed : InvalidToken
InvalidToken: Invalid token.
C password change: Succeeded
C: Failed : InvalidToken
InvalidToken: Invalid token.
...
Why it happens
Case A put the raw token into a query string and read it back the way a reset page would:
...
var link = $"https://app.example.test/reset?email={maria.Email}&token={token}";
var fromLink = QueryHelpers.ParseQuery(new Uri(link).Query)["token"].ToString();
...
The lab generated tokens until one contained a +. After the round trip the first
output line says the + did not survive. A third run also printed the code of the
character that now stood where the first + had been:
A raw token in link, + survived: False
A character now at the first + position: U+0020
...
U+0020 is a space, so the token that reached ResetPasswordAsync was not the one
Identity issued.
Case B passed Maria's token to ResetPasswordAsync(david, ...). Case C generated a
token for Maria, then changed her password with ChangePasswordAsync (which
succeeded), then used the old token. Both returned the same InvalidToken.
The fix
For case A, encode on the way out and decode on the way in:
...
token = await users.GeneratePasswordResetTokenAsync(maria);
var encoded = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(token));
link = $"https://app.example.test/reset?email={maria.Email}&token={encoded}";
var decoded = Encoding.UTF8.GetString(WebEncoders.Base64UrlDecode(
QueryHelpers.ParseQuery(new Uri(link).Query)["token"].ToString()));
Print("Fix A", await users.ResetPasswordAsync(maria, decoded, "Autumn-2026y"));
...
That printed Fix A: Succeeded. Base64Url uses only letters, digits,
- and _, so nothing in the link is changed on the way. For cases B and C there is
nothing to repair in the code: send a fresh reset link for the right user. The lab tested ResetPasswordAsync only; ConfirmEmailAsync was not
run here.
Background, not tested here: a link's query string can be written to server and proxy access logs and kept in browser history, so a reset token should be short-lived and single-use, and the reset page should load no third-party content. Base64Url is a transport encoding only; it protects nothing.
How it was reproduced
A dotnet new web -f net10.0 project on .NET SDK 11.0.100 with
Microsoft.AspNetCore.Identity.EntityFrameworkCore 10.0.10 and
Microsoft.EntityFrameworkCore.Sqlite 10.0.10, Identity registered with
AddDataProtection(), AddIdentityCore<IdentityUser>(),
AddEntityFrameworkStores and AddDefaultTokenProviders(), two fictional
users, and every case run in one startup seed. IdnLab, port 15620, Maria, David, their
.test addresses, the passwords, AppDb and lab.db are the test's;
a real seed reads a password from configuration or user-secrets, never from source. The link host is made up and was never contacted; the URL was only parsed.
The project was built with dotnet build and started three times with
dotnet bin\Debug\net10.0\IdnLab.dll --urls http://127.0.0.1:15620 (it printed
Hosting environment: Production): the first run in a new copy of the base project, the
second also printing the password-change result, the third also printing the character code;
the second and third each moved the previous database file aside first.
Frequently asked
- Why does ResetPasswordAsync return Invalid token?
- In the lab three causes gave the same InvalidToken error: a token whose first + came back from a link as a space (U+0020), a token issued for another user, and a token issued before the user's password changed.
- How do I put an Identity reset token in a URL?
- Encode it with WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(token)) and decode it with WebEncoders.Base64UrlDecode before calling ResetPasswordAsync. In the lab that round trip succeeded.
- Does changing the password invalidate a reset token?
- In the lab, yes: a reset token generated before ChangePasswordAsync succeeded was refused afterwards with InvalidToken.
If no token can be generated at all, see No IUserTwoFactorTokenProvider named 'Default' is registered. More decoded errors in the Fixes category.