예제 #1
0
        public override Task ReplaceClaimAsync(AppIdentityUser identityUser, Claim claim, Claim newClaim, CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (identityUser == null)
            {
                throw new ArgumentNullException(nameof(identityUser));
            }
            if (claim == null)
            {
                throw new ArgumentNullException(nameof(claim));
            }
            if (newClaim == null)
            {
                throw new ArgumentNullException(nameof(newClaim));
            }

            var matchedClaims = _userClaimRepository.GetSpecificClaimByUserId(identityUser.Id, claim.Type, claim.Value);

            foreach (var matchedClaim in matchedClaims)
            {
                matchedClaim.ClaimType  = newClaim.Type;
                matchedClaim.ClaimValue = newClaim.Value;
                _userClaimManager.Save(matchedClaim);
            }

            return(Task.FromResult(IdentityResult.Success));
        }
예제 #2
0
        public override Task AddClaimsAsync(AppIdentityUser identityUser, IEnumerable <Claim> claims, CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (identityUser == null)
            {
                throw new ArgumentNullException(nameof(identityUser));
            }
            if (claims == null)
            {
                throw new ArgumentNullException(nameof(claims));
            }

            var user = _userRepository.Get(identityUser.Id);

            if (user == null)
            {
                return(Task.FromResult(IdentityResult.Failed(
                                           new IdentityError {
                    Code = "user_not_found", Description = $"Cannot find User with id {user.Id}!"
                })));
            }

            foreach (var claim in claims)
            {
                var existingClaims = _userClaimRepository.GetSpecificClaimsByUserId(identityUser.Id, claim.Type, claim.Value);
                if (existingClaims.Length == 0)
                {
                    var newClaim  = CreateUserClaim(identityUser, claim);
                    var userClaim = _mapper.Map <UserClaimDTO>(newClaim);
                    userClaim.Id     = 0;
                    userClaim.UserId = user.Id;
                    _userClaimManager.Save(userClaim);
                }
            }

            return(Task.FromResult(IdentityResult.Success));
        }