Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

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 3

The 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.

Fix
var claims = new List<Claim>
{
    new(JwtRegisteredClaimNames.Sub, user.Id.ToString()),
    new(JwtRegisteredClaimNames.Email, user.Email!),
};
Did this fix it?
Full steps for this cause: check, fix, verify

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).

  • Applies to ASP.NET Core Identity on .NET 10.0.15 and 11.0.0, Microsoft.AspNetCore.Authentication.JwtBearer 11.0.0, IdentityUser<Guid>
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
// 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:

  1. 1The token carries the email or user name in subThe only NameIdentifier value logged is an email address or login name
  2. 2Two claims both become NameIdentifier and the wrong one comes firstThe log prints two NameIdentifier values: one Guid and one email or name
  3. 3UserIdClaimType was changed to a claim that is not the IdNameIdentifier 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.

Check
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.

Fix
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.

Verify
var user = await _userManager.GetUserAsync(User); _logger.LogInformation("Found {Id}", user?.Id);

Confirmed by raw.githubusercontent.com

Did this fix it?

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.

Check
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.

Fix
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.

Verify
dotnet run

Confirmed by learn.microsoft.com

Did this fix it?

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.

Check
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.

Fix
// 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.

Verify
grep -rn 'UserIdClaimType' --include=*.cs . || echo 'default claim type in use'

Confirmed by learn.microsoft.com

Did this fix it?

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.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.