RemoveClaimAsync Identity 2.2

2019-04-11 09:45发布

问题:

I have problem with Identity 2.0 . I'm trying to update claim on a user. So here is my code.

            var UserID = User.Identity.GetUserId();
            var claims = await UserManager.GetClaimsAsync(UserID);

            var displayName = claims.FirstOrDefault(i => i.Type == "DisplayName");
            var isPublic = claims.FirstOrDefault(i => i.Type == "IsPublic");

            if (displayName != null)
                await UserManager.RemoveClaimAsync(UserID, displayName);
            if (isPublic != null)
                await UserManager.RemoveClaimAsync(UserID, isPublic);

            await UserManager.AddClaimAsync(UserID, nClaim("DisplayName", model.DisplayName));
            await UserManager.AddClaimAsync(UserID, nClaim("IsPublic", model.IsPublic.ToString()));

http://prntscr.com/6azxr5 this is what I get. Does anyone have a clue ?

Thanks.

回答1:

This is caused by a know bug which has already been fixed in ASP.Net identity version 2.1.1 https://aspnetidentity.codeplex.com/workitem/2509 Sadly version 2.1.1 is still not available at nuget.org but in the meantime you can download it from here: https://www.myget.org/gallery/aspnetwebstacknightly Once downloaded go to your nuget config in visual studio and add the folder where you downloaded the packages as a new source. Hope it helps