public static void ConfigureClients(IEnumerable<Client> clients, EntityFrameworkServiceOptions options) { using (var db = new ClientConfigurationDbContext(options.ConnectionString, options.Schema)) { if (!db.Clients.Any()) { foreach (var c in clients) { var e = c.ToEntity(); db.Clients.Add(e); } db.SaveChanges(); } } }
protected override void ProcessRecord() { var db = new ClientConfigurationDbContext(this.ConnectionString, this.Schema); var entity = db.Clients.First(s => s.ClientId == this.ClientId); db.Clients.Remove(entity); db.SaveChanges(); }
protected override void ProcessRecord() { var db = new ClientConfigurationDbContext(this.ConnectionString, this.Schema); var entity = db.Clients.Single(c => c.ClientId == this.Client.ClientId); var currentId = entity.Id; var currentclient = entity.ToModel(); var attachedEntry = db.Entry(entity); var newentity = this.Client.ToEntity(); newentity.Id = currentId; attachedEntry.CurrentValues.SetValues(newentity); // Synchronize nav properties - FIXME ugly foreach (var uri in currentclient.RedirectUris.Union(this.Client.RedirectUris).Distinct()) { if (currentclient.RedirectUris.Contains(uri) && !this.Client.RedirectUris.Contains(uri)) { var urientity = entity.RedirectUris.First(x => x.Uri == uri); db.Entry(urientity).State = System.Data.Entity.EntityState.Deleted; entity.RedirectUris.Remove(urientity); } else if (!currentclient.RedirectUris.Contains(uri) && this.Client.RedirectUris.Contains(uri)) entity.RedirectUris.Add(new ClientRedirectUri() { Uri = uri }); } foreach (var uri in currentclient.PostLogoutRedirectUris.Union(this.Client.PostLogoutRedirectUris).Distinct()) { if (currentclient.PostLogoutRedirectUris.Contains(uri) && !this.Client.PostLogoutRedirectUris.Contains(uri)) { var urientity = entity.PostLogoutRedirectUris.First(x => x.Uri == uri); db.Entry(urientity).State = System.Data.Entity.EntityState.Deleted; entity.PostLogoutRedirectUris.Remove(urientity); } else if (!currentclient.PostLogoutRedirectUris.Contains(uri) && this.Client.PostLogoutRedirectUris.Contains(uri)) entity.PostLogoutRedirectUris.Add(new ClientPostLogoutRedirectUri() { Uri = uri }); } foreach (var gt in currentclient.CustomGrantTypeRestrictions.Union(this.Client.CustomGrantTypeRestrictions).Distinct()) { if (currentclient.CustomGrantTypeRestrictions.Contains(gt) && !this.Client.CustomGrantTypeRestrictions.Contains(gt)) { var gtentity = entity.CustomGrantTypeRestrictions.First(x => x.GrantType == gt); db.Entry(gtentity).State = System.Data.Entity.EntityState.Deleted; entity.CustomGrantTypeRestrictions.Remove(gtentity); } else if (!currentclient.CustomGrantTypeRestrictions.Contains(gt) && this.Client.CustomGrantTypeRestrictions.Contains(gt)) entity.CustomGrantTypeRestrictions.Add(new ClientGrantTypeRestriction() { GrantType = gt }); } foreach (var scope in currentclient.ScopeRestrictions.Union(this.Client.ScopeRestrictions).Distinct()) { if (currentclient.ScopeRestrictions.Contains(scope) && !this.Client.ScopeRestrictions.Contains(scope)) { var scopenetity = entity.ScopeRestrictions.First(x => x.Scope == scope); db.Entry(scopenetity).State = System.Data.Entity.EntityState.Deleted; entity.ScopeRestrictions.Remove(scopenetity); } else if (!currentclient.ScopeRestrictions.Contains(scope) && this.Client.ScopeRestrictions.Contains(scope)) entity.ScopeRestrictions.Add(new ClientScopeRestriction() { Scope = scope }); } foreach (var provider in currentclient.IdentityProviderRestrictions.Union(this.Client.IdentityProviderRestrictions).Distinct()) { if (currentclient.IdentityProviderRestrictions.Contains(provider) && !this.Client.ScopeRestrictions.Contains(provider)) { var idpentity = entity.IdentityProviderRestrictions.First(x => x.Provider == provider); db.Entry(idpentity).State = System.Data.Entity.EntityState.Deleted; entity.IdentityProviderRestrictions.Remove(idpentity); } else if (!currentclient.IdentityProviderRestrictions.Contains(provider) && this.Client.IdentityProviderRestrictions.Contains(provider)) entity.IdentityProviderRestrictions.Add(new ClientIdPRestriction() { Provider = provider }); } foreach (var secret in currentclient.ClientSecrets.Union(this.Client.ClientSecrets).Distinct()) { if (currentclient.ClientSecrets.Any(x => x.Value == secret.Value) && !this.Client.ClientSecrets.Any(x => x.Value == secret.Value)) entity.ClientSecrets.Remove(entity.ClientSecrets.First(x => x.Value == secret.Value)); else if (!currentclient.ClientSecrets.Any(x => x.Value == secret.Value) && this.Client.ClientSecrets.Any(x => x.Value == secret.Value)) entity.ClientSecrets.Add(new Thinktecture.IdentityServer.EntityFramework.Entities.ClientSecret { ClientSecretType = secret.ClientSecretType, Description = secret.ClientSecretType, Expiration = secret.Expiration, Value = secret.Value }); } foreach (var claim in currentclient.Claims.Union(this.Client.Claims).Distinct()) { if (currentclient.Claims.Any(x => x.Type == claim.Type) && !this.Client.Claims.Any(x => x.Type == claim.Type)) entity.Claims.Remove(entity.Claims.First(x => x.Type == claim.Type && x.Value == claim.Value)); else if (!currentclient.Claims.Any(x => x.Type == claim.Type) && this.Client.Claims.Any(x => x.Type == claim.Type)) entity.Claims.Add(new Thinktecture.IdentityServer.EntityFramework.Entities.ClientClaim() { Type = claim.Type, Value = claim.Value }); } try { db.SaveChanges(); } catch (System.Data.Entity.Infrastructure.DbUpdateException dbue) { WriteError(new ErrorRecord(dbue.InnerException, "DbUpdateException", ErrorCategory.WriteError, entity)); throw dbue; } WriteObject(entity.ToModel()); }
protected override void ProcessRecord() { var db = new ClientConfigurationDbContext(this.ConnectionString, this.Schema); var client = new Thinktecture.IdentityServer.Core.Models.Client { ClientId = this.ClientId, ClientName = this.ClientName, ClientSecrets = (from s in this.ClientSecrets select new Thinktecture.IdentityServer.Core.Models.ClientSecret(s)).ToList(), Flow = this.Flow, Enabled = this.Enabled, RequireConsent = this.RequireConsent, AllowRememberConsent = this.AllowRememberConsent, ClientUri = this.ClientUri, LogoUri = this.LogoUri, RedirectUris = this.RedirectUris, PostLogoutRedirectUris = this.PostLogoutUris, ScopeRestrictions = this.ScopeRestrictions, IdentityProviderRestrictions = this.IdentityProviderRestrictions, AccessTokenType = this.AccessTokenType, AccessTokenLifetime = this.TokensLifetime, IdentityTokenLifetime = this.TokensLifetime, AuthorizationCodeLifetime = this.TokensLifetime // bad }; db.Clients.Add(client.ToEntity()); db.SaveChanges(); WriteObject(client); }