public async Task CreateAsync(IdentityScopeModel model) { var scope = new Duende.IdentityServer.Models.IdentityResource() { Name = model.Name, DisplayName = model.DisplayName?.Trim() }; var claims = model.UserClaims?.Split(' ', StringSplitOptions.RemoveEmptyEntries).ToArray() ?? Enumerable.Empty <string>(); if (claims.Any()) { scope.UserClaims = claims.ToList(); } _context.IdentityResources.Add(scope.ToEntity()); await _context.SaveChangesAsync(); }
public async Task UpdateAsync(IdentityScopeModel model) { var scope = await _context.IdentityResources .Include(x => x.UserClaims) .SingleOrDefaultAsync(x => x.Name == model.Name); if (scope == null) { throw new Exception("Invalid Identity Scope"); } if (scope.DisplayName != model.DisplayName) { scope.DisplayName = model.DisplayName?.Trim(); } var claims = model.UserClaims?.Split(' ', StringSplitOptions.RemoveEmptyEntries).ToArray() ?? Enumerable.Empty <string>(); var currentClaims = (scope.UserClaims.Select(x => x.Type) ?? Enumerable.Empty <String>()).ToArray(); var claimsToAdd = claims.Except(currentClaims).ToArray(); var claimsToRemove = currentClaims.Except(claims).ToArray(); if (claimsToRemove.Any()) { scope.UserClaims.RemoveAll(x => claimsToRemove.Contains(x.Type)); } if (claimsToAdd.Any()) { scope.UserClaims.AddRange(claimsToAdd.Select(x => new IdentityResourceClaim { Type = x, })); } await _context.SaveChangesAsync(); }