public async Task AddClaimAsync(User user, Claim claim, CancellationToken cancellationToken = default(CancellationToken))
 {
     cancellationToken.ThrowIfCancellationRequested();
     if (user == (User)null)
         throw new ArgumentNullException("user");
     if (claim == (Claim)null)
         throw new ArgumentNullException("claim");
     User dbUser = await this._userRepository.GetFirstOrDefaultAsync((x => x.Id == user.Id), cancellationToken, (x => x.Claims));
     if (dbUser == (User)null)
         throw new Exception(string.Format(Messages.UserNotFoundException, ("User id=" + user.Id)));
     if (dbUser.Claims == null)
         dbUser.Claims = (ICollection<Claim>)new List<Claim>();
     dbUser.Claims.Add(claim);
     this._userRepository.SetModified(dbUser);
     await this._userRepository.UnitOfWork.CommitAsync();
 }
        public async Task RemoveClaimAsync(User user, Claim claim, CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();

            if (user == (User)null)
                throw new ArgumentNullException("user");
            if (claim == (Claim)null)
                throw new ArgumentNullException("claim");
            User dbUser = await this._userRepository.GetFirstOrDefaultAsync((x => x.Id == user.Id), cancellationToken, (x => x.Claims));
            if (dbUser == (User)null)
                throw new Exception(string.Format(Messages.UserNotFoundException, ("User id=" + user.Id)));
            if (dbUser.Claims != null)
            {
                Claim clm = (Claim)null;
                clm = (claim.Id == Guid.Empty) ?
                     dbUser.Claims.FirstOrDefault(x => x.ClaimType != null && x.ClaimType == claim.ClaimType && x.ClaimValue != null && x.ClaimValue == claim.ClaimValue) :
                     dbUser.Claims.FirstOrDefault(x => x.Id == claim.Id);
                if (clm == (Claim)null)
                    throw new Exception(Messages.RemoveNonExistingClaimException);
                dbUser.Claims.Remove(clm);
                this._userRepository.SetModified(dbUser);
                await this._userRepository.UnitOfWork.CommitAsync();
            }
        }
        public static Claim ToDomainClaim(this System.Security.Claims.Claim model)
        {
            Claim userClaim1 = (Claim)null;
            if (model != null)
            {
                userClaim1 = new Claim();
                userClaim1.ClaimType = model.Type;
                userClaim1.ClaimValue = model.Value;
                userClaim1.Description = model.ValueType;
            }

            return userClaim1;
        }