0

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. Exception: "23505: duplicate key value violates unique constraint \"PK_AspNetUserClaims\"\n\nDETAIL: Key (\"Id\")=(6532) already exists."

ASP.Net Core 7.0.105

Kok How Teh
  • 3,298
  • 6
  • 47
  • 85
  • Postgresql ? [Could you pls check the link first.](https://itecnote.com/tecnote/postgresql-error-23505-duplicate-key-value-violates-unique-constraint-foo_column_key-not-primary-key/) – Jason Pan Jun 01 '23 at 07:06

0 Answers0