Exemple #1
0
        public async Task AddClaims(string userId, ClaimModel[] models)
        {
            long u          = long.Parse(userId);
            var  claimQuery = from userClaim in _db.Set <Claim>()
                              where userClaim.UserId == u
                              select userClaim;

            var userClaims = await claimQuery.ToListAsync();

            //Get Existing Claims
            var existingQueries = from claim in userClaims
                                  join model in models
                                  on claim.Type equals model.Type
                                  select new { claim, model };

            //Update Existing Claims
            foreach (var record in existingQueries)
            {
                record.claim.Value = record.model.Value;
            }

            //Add new Claims
            var newClaims = models.Except(existingQueries.Select(r => r.model));

            foreach (var newClaim in newClaims)
            {
                _db.Add(new Claim
                {
                    UserId = long.Parse(userId),
                    Type   = newClaim.Type,
                    Value  = newClaim.Value
                });
            }

            _db.SaveChanges();
        }