aspnetcore: `UserManager.AddClaimAsync` keeps throwing duplicate PK exception for new claims

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

The following code snippet:

Claim claim = claims.FirstOrDefault(c => c.Type == "roles");
if (claim != null)
    await _userManager.RemoveClaimAsync(appUser, claim);
StringBuilder sb = new StringBuilder();
if (claim != null && !string.IsNullOrEmpty(claim.Value) && !claim.Value.Contains("buyer"))
    sb.Append($"{claim.Value} buyer");
else
    sb.Append("buyer");
try {
    await _userManager.AddClaimAsync(appUser, new Claim("roles", sb.ToString()));
} catch (Exception e) {
    _logger.LogError($"{nameof(CreateOrUpdateUser)} Exception! {e}");
}

keeps throwing the exception. I have checked the DB this user does NOT have roles claim at all

Expected Behavior

Successfully added new claim to the user.

Steps To Reproduce

No response

Exceptions (if any)

"23505: duplicate key value violates unique constraint \"PK_AspNetUserClaims\"\n\nDETAIL: Key (\"Id\")=(6532) already exists."

.NET Version

7.0.105

Anything else?

.NET SDK:
 Version:   7.0.105
 Commit:    e1bc5e001c

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  23.04
 OS Platform: Linux
 RID:         ubuntu.23.04-x64
 Base Path:   /usr/lib/dotnet/sdk/7.0.105/

Host:
  Version:      7.0.5
  Architecture: x64
  Commit:       8042d61b17

.NET SDKs installed:
  7.0.105 [/usr/lib/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 7.0.5 [/usr/lib/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 7.0.5 [/usr/lib/dotnet/shared/Microsoft.NETCore.App]

Other architectures found:
  None

Environment variables:
  DOTNET_ROOT       [/usr/lib/dotnet]

global.json file:
  Not found

Learn more:
  https://aka.ms/dotnet/info

Download .NET:
  https://aka.ms/dotnet/download

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Comments: 16 (12 by maintainers)

Most upvoted comments

Thanks for helping with this investigation, @martincostello! @khteh was @martincostello’s last response help you with identifying the problem, or is there more you need help with?

Searching for PK_AspNetUserClaims in the repo code comes up with migrations that look like this:

https://github.com/dotnet/aspnetcore/blob/be14dda052a54373e542a9978f172d1900f15486/src/Identity/samples/IdentitySample.Mvc/Data/Migrations/00000000000000_CreateIdentitySchema.cs#L97-L106

This seems to suggest its failing to insert a row with any content in the AspNetUserClaims table because there’s already a row with a primary key of 6532.

If you do a SQL select for that on your database does it find anything? I haven’t tested this query, so might not be 100% correct:

select * from AspNetUserClaims where Id = 6532;

Feels like something’s off with the auto-incrementing ID for that table on your database/code causing it to incorrectly try and add a duplicate.

Looking at the exception message, it seems like the constraint is complaining about the primary key itself, rather than the value of the claim itself.

Have you done anything to customise the database schema for the underlying store where you keep your data?