System.InvalidOperationException: User security stamp cannot be null. was thrown by GeneratePasswordResetTokenAsync for a user row that had been inserted by hand without a SecurityStamp. Give the user a stamp with UserManager.UpdateSecurityStampAsync(user) before the call; setting one when the row is written is the other route, not run here.

The row here was written with plain SQL, the way an import script or a hand-written seed might do it. In the same run, a second user made with UserManager.CreateAsync came back with a stamp, and the same token call worked for it.

The error

Console output: the first line on standard output, the exception on the error stream:

Found: chen.wei@example.test, SecurityStamp is null: True
Unhandled exception. System.InvalidOperationException: User security stamp cannot be null.
   at Microsoft.AspNetCore.Identity.UserManager`1.GetSecurityStampAsync(TUser user)
   at Microsoft.AspNetCore.Identity.DataProtectorTokenProvider`1.GenerateAsync(String purpose, UserManager`1 manager, TUser user)
...

Why it happens

The seed wrote the user straight into the table, leaving SecurityStamp out:

...
    await db.Database.ExecuteSqlRawAsync(
        "INSERT INTO AspNetUsers (Id, UserName, NormalizedUserName, Email, NormalizedEmail, EmailConfirmed, " +
        "PhoneNumberConfirmed, TwoFactorEnabled, LockoutEnabled, AccessFailedCount) " +
        "VALUES ('u-1001', 'chen.wei@example.test', 'CHEN.WEI@EXAMPLE.TEST', 'chen.wei@example.test', " +
        "'CHEN.WEI@EXAMPLE.TEST', 1, 0, 0, 1, 0)");
...

FindByNameAsync found the user and the output confirms the stamp was null. The stack trace shows the token provider asking GetSecurityStampAsync for the stamp to build the token, and that method refusing a null one. Finding the user and reading its fields worked; the call that needs the stamp is where it stopped. Other UserManager calls were not tried with the null stamp in this lab; only the reset token was asked for, so that is the one call this post can vouch for, and the stamp fix was checked against that call alone.

The fix

...
    var user = await users.FindByNameAsync("chen.wei@example.test");
    Console.WriteLine($"Found: {user?.UserName}, SecurityStamp is null: {user?.SecurityStamp is null}");
    if (user!.SecurityStamp is null)
        Console.WriteLine($"Update stamp: {await users.UpdateSecurityStampAsync(user)}");
    var token = await users.GeneratePasswordResetTokenAsync(user);
...

That printed Update stamp: Succeeded and Reset token length: 200. A third run added a user created with CreateAsync and asked for the same token:

CreateAsync user: Succeeded, SecurityStamp is null: False
Reset token length for it: 240
...

For new rows, the cleaner fix is at the source: create users through CreateAsync, or write a stamp (a new GUID string, for example) when you insert them yourself; that variant was not run here. The HasData seeding route from the list this lab started with was not tested either.

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(). IdnLab, port 15620, the users, the id u-1001, the password, AppDb and lab.db are the test's; a real seed reads a password from configuration or user-secrets, never from source. Three runs, each built with dotnet build and started with dotnet bin\Debug\net10.0\IdnLab.dll --urls http://127.0.0.1:15620: the broken one in a new copy of the base project (the evidence records no lab.db before it), the fix, and the fix with the CreateAsync user added, each later run after moving the previous file aside.

Frequently asked

What does User security stamp cannot be null mean?
The user row has no SecurityStamp. In the lab a user inserted with plain SQL had a null stamp, and GeneratePasswordResetTokenAsync threw this InvalidOperationException.
How do I fix a null security stamp for an existing user?
Call UserManager.UpdateSecurityStampAsync(user) before the call that needs it. In the lab that returned Succeeded and the reset token was then generated.
How do I avoid null security stamps when seeding users?
Create users through UserManager.CreateAsync: in the lab a user made that way had a stamp and the same token call worked for it. Writing a stamp yourself in SQL or HasData was not tested here.

If the token call fails before it ever reads the stamp, see No IUserTwoFactorTokenProvider named 'Default' is registered. More decoded errors in the Fixes category.