Username 'admin' is already taken. is the DuplicateUserName error
that UserManager.CreateAsync returned when a startup seed ran a second time on the
same database. Look the user up with FindByNameAsync first and create it only
when the lookup returns null.
With RequireUniqueEmail switched on, the same second run also returned
DuplicateEmail. Both are results, not exceptions, so a seed that ignores the
result just carries on and the app starts as if nothing happened.
The error
Console output of the second start, the seed printing each error's code and description:
Seed: Failed : DuplicateUserName,DuplicateEmail
DuplicateUserName: Username 'admin' is already taken.
DuplicateEmail: Email 'admin@example.test' is already taken.
...
Why it happens
The seed ran on every start and called CreateAsync for the same user name each
time. The first start printed Seed: Succeeded. The second
start used the same file, the user was already in it, and CreateAsync refused to
add another one with the same name. The email error appeared because this project registered
Identity with options.User.RequireUniqueEmail = true.
A clean rerun with RequireUniqueEmail left at its default and no lookup, on a new
database file (lab-defaults.db, which did not exist before), printed
Seed: Succeeded on the first start and only the user name error on the second:
Seed: Failed : DuplicateUserName
DuplicateUserName: Username 'admin' is already taken.
...
So the email code depends on the option, and the user name code appeared in both runs.
The fix
...
if (await users.FindByNameAsync("admin") is null)
{
var result = await users.CreateAsync(
new IdentityUser { UserName = "admin", Email = "admin@example.test" },
"Spring-2026x");
Console.WriteLine($"Seed: {result}");
foreach (var error in result.Errors)
Console.WriteLine($" {error.Code}: {error.Description}");
}
else
{
Console.WriteLine("Seed: admin exists, skipped");
}
...
A third start on the same database printed Seed: admin exists, skipped, and the
same code on a fresh file printed Seed: Succeeded: one fresh start and one later
start ran with the fix. The user admin, its .test address and its password
are the test's; a real seed reads the password from configuration or user-secrets, never from
source. Seeding roles has its own pitfall, covered in
'Role ADMIN does
not exist'.
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
AddIdentityCore<IdentityUser>(options => options.User.RequireUniqueEmail = true)
and a seed in a scope after builder.Build(); AppDb and lab.db
are the test's names. Every start was dotnet bin\Debug\net10.0\IdnLab.dll --urls http://127.0.0.1:15620
after a dotnet build (it printed Hosting environment: Production). The app
was started twice on one SQLite file, then the fixed seed on that file and on a fresh one. The
default options case was two starts with AddIdentityCore<IdentityUser>(), no
lookup, on the new file lab-defaults.db. Two earlier default-options runs in the
evidence ran on a file that already held admin and are not used here.
Frequently asked
- Why does my Identity seed say the username is already taken?
- The seed runs on every start and the user was created on an earlier start. Call FindByNameAsync first and create the user only when it returns null.
- What is the difference between DuplicateUserName and DuplicateEmail?
- DuplicateUserName is returned for a user name that exists. DuplicateEmail appeared in the lab only when RequireUniqueEmail was set to true; with the default options the second create returned DuplicateUserName alone.
- Does UserManager.CreateAsync throw for a duplicate user?
- No. It returned an IdentityResult with Succeeded false and the error codes in Errors, and the app went on starting.
A seed that fails on the password rules instead is covered in Passwords must have at least one non alphanumeric character. More decoded errors in the Fixes category.