コード例 #1
0
        public override async Task AddClaimsAsync(DomainUser user, IEnumerable <Claim> claims, CancellationToken cancellationToken = default)
        {
            string baseErrMsg = "DomainUserStore.AddClaimsAsync failed with {Code}: {Description}";
            var    userClaims = claims.Select(c => new IdentityUserClaim <int> {
                UserId = user.Id, ClaimType = c.Type, ClaimValue = c.Value
            });

            try {
                var existingClaims = await _dbContext.Set <IdentityUserClaim <int> >()
                                     .Where(uc => uc.UserId == user.Id)
                                     .Select(uc => new IdentityUserClaim <int> {
                    UserId = uc.UserId, ClaimType = uc.ClaimType, ClaimValue = uc.ClaimValue
                })
                                     .ToListAsync(cancellationToken);

                var claimsToAdd = userClaims.Except(existingClaims);
                _dbContext.Set <IdentityUserClaim <int> >().AddRange(claimsToAdd);
                await _dbContext.SaveChangesAsync(cancellationToken);
            } catch (DbUpdateException ex) {
                var err = ErrorDescriber.DbUpdateException(ex);
                _logger.LogError(baseErrMsg, err.Code, err.Description);
                throw new ApplicationException(err.Description, ex);
            }
        }