UserManager.GetUserAsync throws a Guid format error for a signed-in user
Your error
System.FormatException: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).Fix it fast
Most common cause, 1 of 3The token carries the email or user name in sub
When you create the token, put the user's Id in sub and move the email to its own claim.
var claims = new List<Claim>
{
new(JwtRegisteredClaimNames.Sub, user.Id.ToString()),
new(JwtRegisteredClaimNames.Email, user.Email!),
};Short answer
UserManager.GetUserAsync reads the NameIdentifier claim and parses it as the user's key. With IdentityUser<Guid>, a claim holding an email or user name fails with "Guid should contain 32 digits with 4 dashes". Usually the JWT puts the email in sub or nameid, both mapped to NameIdentifier. Issue the token with the user's Id in sub (ASP.NET Core 10 and 11).
Which cause do you have?
Print every NameIdentifier claim on the request that fails. One value that is an email or name points to cause 1. Two values point to cause 2. A correct Guid here, while the error remains, points to cause 3.
// inside the failing controller action
foreach (var c in User.FindAll(ClaimTypes.NameIdentifier))
_logger.LogInformation("NameIdentifier: {Value}", c.Value);Then find the line you see in this list and open its fix:
- 1The token carries the email or user name in sub
The only NameIdentifier value logged is an email address or login name - 2Two claims both become NameIdentifier and the wrong one comes first
The log prints two NameIdentifier values: one Guid and one email or name - 3UserIdClaimType was changed to a claim that is not the Id
NameIdentifier holds a correct Guid, but the code sets ClaimsIdentity.UserIdClaimType to Email, Name or a custom claim
What does the error look like?
System.FormatException: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).
Cause 1 of 3
The token carries the email or user name in sub
You seeThe only NameIdentifier value logged is an email address or login name
Check
GetUserAsync fails with the Guid format error when the identifier claim is not the user's Id. UserManager takes the value of IdentityOptions.ClaimsIdentity.UserIdClaimType, which defaults to NameIdentifier, and the user store converts it to TKey with a TypeConverter. The JWT handler maps the sub claim to NameIdentifier, so whatever your token code put in sub reaches Guid parsing.
grep -rnE 'JwtRegisteredClaimNames\.Sub|"sub"' --include=*.cs .Fix
When you create the token, put the user's Id in sub and move the email to its own claim. The Id string of an IdentityUser<Guid> is the Guid the store expects.
var claims = new List<Claim>
{
new(JwtRegisteredClaimNames.Sub, user.Id.ToString()),
new(JwtRegisteredClaimNames.Email, user.Email!),
};Verify
Sign in again to get a new token. The log shows a Guid, and GetUserAsync returns the user.
var user = await _userManager.GetUserAsync(User); _logger.LogInformation("Found {Id}", user?.Id);Confirmed by raw.githubusercontent.com
Cause 2 of 3
Two claims both become NameIdentifier and the wrong one comes first
You seeThe log prints two NameIdentifier values: one Guid and one email or name
Check
In the inbound claim map, nameid and sub both translate to ClaimTypes.NameIdentifier. A token that sends nameid with the email and sub with the Id therefore holds two identifier claims, and UserManager uses FindFirstValue, which returns only the first one.
grep -rnE 'JwtRegisteredClaimNames\.NameId|ClaimTypes\.NameIdentifier' --include=*.cs .Fix
Send only one identifier claim. Remove nameid from the token, or turn off inbound mapping so sub keeps its own name and point Identity at it.
builder.Services.AddAuthentication()
.AddJwtBearer(options =>
{
options.MapInboundClaims = false;
});
builder.Services.Configure<IdentityOptions>(options =>
options.ClaimsIdentity.UserIdClaimType = "sub");Verify
The first-step log shows a single Guid, and the lookup no longer throws.
dotnet runConfirmed by learn.microsoft.com
Cause 3 of 3
UserIdClaimType was changed to a claim that is not the Id
You seeNameIdentifier holds a correct Guid, but the code sets ClaimsIdentity.UserIdClaimType to Email, Name or a custom claim
Check
ClaimsIdentityOptions.UserIdClaimType decides which claim GetUserId and GetUserAsync read; Microsoft's API reference gives NameIdentifier as its default. Pointing it at the email claim, often to make a login screen work, feeds the email into FindByIdAsync.
grep -rn 'UserIdClaimType' --include=*.cs .Fix
Leave UserIdClaimType on the claim that holds the Id. To look a user up by email, call FindByEmailAsync instead of changing the option.
// remove: options.ClaimsIdentity.UserIdClaimType = ClaimTypes.Email;
var email = User.FindFirstValue(ClaimTypes.Email);
var user = email is null ? null : await _userManager.FindByEmailAsync(email);Verify
GetUserAsync works again, and email lookups go through FindByEmailAsync.
grep -rn 'UserIdClaimType' --include=*.cs . || echo 'default claim type in use'Confirmed by learn.microsoft.com
How do I stop it coming back?
- Build every token in one factory method that always writes user.Id to sub, so no endpoint invents its own claim set.
- Add an integration test that signs in, calls an endpoint using GetUserAsync and asserts a non-null user.
- Decide once whether inbound claim mapping is on, and set MapInboundClaims and UserIdClaimType together in Program.cs.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: Raheeb, Loade Driver, Dhero.