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
c # identity asp.net-core asp.net-authorization


source to share


No one has answered this question yet

Check out similar questions:

8
ASP.NET Identity: Update External Claims After Authorization
6
Dynamic User Requests in ASP.NET Identity EF
five
Clarifying Identity Authorization: Using Claims as Roles, Roles and Claims or Role Requirements
3
how to get another user's claims using ASP.NET Core
2
ASP.NET Identity Does Not "Save" Updated Claim
2
AspNet Identity Core - Custom Login Claims
1
ASP.NET id does not load any claims
1
ASP.NET Identity - How to Manage Claims?
0
How do I change the access token requirements received from WS-FED in Core.net?
0
ASP.NET core id - how to add new claims after initial login



All Articles
Loading...
X
Show
Funny
Dev
Pics