Editing asp.net core id requirements instead of adding them

I am using external logins for my main website asp.net core mvc and when the login ends I want to keep the social information they used to login (like username, profile picture, etc.) as claims to user identity ...

So, I run a code like this in my account controller after a successful login:

    var authClaim = user.Claims.FirstOrDefault(c => c.ClaimType == authToken.Name);
    if (authClaim == null)
          externalClaims.Add(new Claim(authToken.Name, authToken.Value));
    else
          authClaim.ClaimValue = authToken.Value;

    if (externalClaims.Any())
          await _userManager.AddClaimsAsync(user, externalClaims);

await _userManager.UpdateAsync(user);

      

however, when I try to do this, the claims table duplicates the claims rather than updating them as I expected, so every time a user logs in, I get another set of table entries for that user that are identical to the last one.

Do I need to remove them and re-add them every time I log in? if not, how can I update existing claims without duplicating them?

I do this every time I log in, if the user has changed their profile or other information (and to make sure I have the most recent token).

I am also wondering why I am adding claims to the main user id. The user has an ExternalClaims property , but it's empty and there doesn't seem to be any way to update it. It seems to me that this is the best place to post these third party claims, but I cannot for the life of me find a way to change it ...

anyway i am sure i will have the same problem if i use this code so the correct course is to remove the claim on login and always add it to the new version or should i do something differently ?

+3


source to share





All Articles