New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with call UpdateSecurityStampAsync #2069

Open
meloman001 opened this Issue Nov 13, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@meloman001

meloman001 commented Nov 13, 2018

I need to update user security stamp for logout user from system on all devices . For this purpose i wanna use UpdateSecurityStampAsync, but i get an exception and i cannot fix it

InvalidOperationException: The instance of entity type 'ApplicationUser' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached.

Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap<TKey>.ThrowIdentityConflict(InternalEntityEntry entry)
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap<TKey>.Add(TKey key, InternalEntityEntry entry, bool updateDuplicate)
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap<TKey>.Add(TKey key, InternalEntityEntry entry)
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NullableKeyIdentityMap<TKey>.Add(InternalEntityEntry entry)
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.StartTracking(InternalEntityEntry entry)
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState oldState, EntityState newState, bool acceptChanges)
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState entityState, bool acceptChanges, Nullable<EntityState> forceStateWhenUnknownKey)
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.PaintAction(EntityEntryGraphNode node, bool force)
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityEntryGraphIterator.TraverseGraph<TState>(EntityEntryGraphNode node, TState state, Func<EntityEntryGraphNode, TState, bool> handleNode)
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.AttachGraph(InternalEntityEntry rootEntry, EntityState entityState, bool forceStateWhenUnknownKey)
Microsoft.EntityFrameworkCore.DbContext.SetEntityState(InternalEntityEntry entry, EntityState entityState)
Microsoft.EntityFrameworkCore.DbContext.SetEntityState<TEntity>(TEntity entity, EntityState entityState)
Microsoft.EntityFrameworkCore.DbContext.Attach<TEntity>(TEntity entity)
Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore<TUser, TRole, TContext, TKey, TUserClaim, TUserRole, TUserLogin, TUserToken, TRoleClaim>.UpdateAsync(TUser user, CancellationToken cancellationToken)
Microsoft.AspNetCore.Identity.UserManager<TUser>.UpdateUserAsync(TUser user)
Microsoft.AspNetCore.Identity.UserManager<TUser>.UpdateSecurityStampAsync(TUser user)
WebAIS.Services.UserManager.UserManagerService.UpdateSecurityStampAsync(ApplicationUser user) in UserManagerService.cs
/// Метод обновляет SecurityStamp для пользователя
        /// </summary>
        /// <param name="user">Пользователь</param>
        public async Task<IdentityResult> UpdateSecurityStampAsync(ApplicationUser user)
        {
            var result = await _userManager.UpdateSecurityStampAsync(user);
            await ResetUserRedisCacheAsync(user);
            return result;
        }

I changed the type of used services to the scoped and singleton, but this did not solve the problem

@ajcvickers ajcvickers transferred this issue from aspnet/EntityFrameworkCore Nov 14, 2018

@blowdart

This comment has been minimized.

Member

blowdart commented Nov 19, 2018

What does ResetUserRedisCacheAsync to? When you update a user we'd expect you to update the user via the user manager. It looks like something is changing the user outside of the manager, thus you're getting that race

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment