public async Task <Unit> Handle(UpdateClientCommand request, CancellationToken cancellationToken) { var client = await _configurationRepository .All <ID4_EF_E.Client>() .Include(c => c.AllowedCorsOrigins) .Include(c => c.AllowedGrantTypes) .Include(c => c.AllowedScopes) .Include(c => c.Claims) .Include(c => c.ClientSecrets) .Include(c => c.IdentityProviderRestrictions) .Include(c => c.PostLogoutRedirectUris) .Include(c => c.Properties) .Include(c => c.RedirectUris) .SingleAsync(c => c.Id == request.Id, cancellationToken); client.ClientSecrets.RemoveAll(s => !request.ClientSecrets.Select(rs => rs.Value.Sha256()).Contains(s.Value)); foreach (var clientSecret in request.ClientSecrets.Distinct()) { var shaSecret = clientSecret.Value.Sha256(); var secret = client.ClientSecrets.FirstOrDefault(s => s.Value == shaSecret); if (secret != null) { secret.Type = clientSecret.Type; secret.Value = shaSecret; secret.Description = clientSecret.Description; secret.Expiration = clientSecret.Expiration; } else { client.ClientSecrets.Add(new ID4_EF_E.ClientSecret { Type = clientSecret.Type, Value = shaSecret, Description = clientSecret.Description, Created = DateTime.UtcNow, Expiration = clientSecret.Expiration }); } } client.Claims.RemoveAll(cc => !request.Claims.Any(rc => rc.Type == cc.Type && rc.Value == cc.Value)); foreach (var claim in request.Claims.Distinct()) { if (!client.Claims.Any(c => c.Value == claim.Value && c.Type != claim.Type)) { client.Claims.Add(new ID4_EF_E.ClientClaim { Type = claim.Type, Value = claim.Value }); } } client.AllowedCorsOrigins.RemoveAll(o => !request.AllowedCorsOrigins.Contains(o.Origin)); foreach (var origin in request.AllowedCorsOrigins.Distinct()) { if (!client.AllowedCorsOrigins.Any(o => o.Origin == origin)) { client.AllowedCorsOrigins.Add(new ID4_EF_E.ClientCorsOrigin { Origin = origin }); } } client.AllowedGrantTypes.RemoveAll(gt => !request.AllowedGrantTypes.Contains(gt.GrantType)); foreach (var grantType in request.AllowedGrantTypes.Distinct()) { if (!client.AllowedGrantTypes.Any(gt => gt.GrantType == grantType)) { client.AllowedGrantTypes.Add(new ID4_EF_E.ClientGrantType { GrantType = grantType }); } } client.AllowedScopes.RemoveAll(s => !request.AllowedScopes.Contains(s.Scope)); foreach (var scope in request.AllowedScopes.Distinct()) { if (!client.AllowedScopes.Any(s => s.Scope == scope)) { client.AllowedScopes.Add(new ID4_EF_E.ClientScope { Scope = scope }); } } client.IdentityProviderRestrictions.RemoveAll(p => !request.IdentityProviderRestrictions.Contains(p.Provider)); foreach (var provider in request.IdentityProviderRestrictions.Distinct()) { if (!client.IdentityProviderRestrictions.Any(p => p.Provider == provider)) { client.IdentityProviderRestrictions.Add(new ID4_EF_E.ClientIdPRestriction { Provider = provider }); } } client.PostLogoutRedirectUris.RemoveAll(p => !request.PostLogoutRedirectUris.Contains(p.PostLogoutRedirectUri)); foreach (var postLogoutRedirectUri in request.PostLogoutRedirectUris.Distinct()) { if (!client.PostLogoutRedirectUris.Any(p => p.PostLogoutRedirectUri == postLogoutRedirectUri)) { client.PostLogoutRedirectUris.Add(new ID4_EF_E.ClientPostLogoutRedirectUri { PostLogoutRedirectUri = postLogoutRedirectUri }); } } client.Properties.RemoveAll(cp => !request.Properties.Any(rp => rp.Key == cp.Key && rp.Value == cp.Value)); foreach (var property in request.Properties.Distinct()) { var currentProperty = client.Properties.FirstOrDefault(p => p.Key == property.Key); if (currentProperty != null) { currentProperty.Value = property.Value; } else { client.Properties.Add(new ID4_EF_E.ClientProperty { Key = property.Key, Value = property.Value }); } } client.RedirectUris.RemoveAll(p => !request.RedirectUris.Contains(p.RedirectUri)); foreach (var redirectUri in request.RedirectUris.Distinct()) { if (!client.RedirectUris.Any(p => p.RedirectUri == redirectUri)) { client.RedirectUris.Add(new ID4_EF_E.ClientRedirectUri { RedirectUri = redirectUri }); } } await _configurationRepository.BulkSaveChangesAsync(cancellationToken); return(Unit.Value); }