using Duende.IdentityServer.EntityFramework.DbContexts; using Duende.IdentityServer.EntityFramework.Mappers; using Duende.IdentityServer.Models; using Microsoft.EntityFrameworkCore; using Serilog; namespace Microser.IdS { public class SeedData { public static void EnsureSeedData(WebApplication app) { using (var scope = app.Services.GetRequiredService().CreateScope()) { scope.ServiceProvider.GetRequiredService().Database.Migrate(); var context = scope.ServiceProvider.GetRequiredService(); context.Database.Migrate(); EnsureSeedData(context); } } private static void EnsureSeedData(ConfigurationDbContext context) { if (!context.Clients.Any()) { Log.Debug("Clients being populated"); foreach (var client in Config.Clients.ToList()) { context.Clients.Add(client.ToEntity()); } context.SaveChanges(); } else { Log.Debug("Clients already populated"); } if (!context.IdentityResources.Any()) { Log.Debug("IdentityResources being populated"); foreach (var resource in Config.IdentityResources.ToList()) { context.IdentityResources.Add(resource.ToEntity()); } context.SaveChanges(); } else { Log.Debug("IdentityResources already populated"); } if (!context.ApiScopes.Any()) { Log.Debug("ApiScopes being populated"); foreach (var resource in Config.ApiScopes.ToList()) { context.ApiScopes.Add(resource.ToEntity()); } context.SaveChanges(); } else { Log.Debug("ApiScopes already populated"); } if (!context.IdentityProviders.Any()) { Log.Debug("OIDC IdentityProviders being populated"); context.IdentityProviders.Add(new OidcProvider { Scheme = "demoidsrv", DisplayName = "IdentityServer", Authority = "https://demo.duendesoftware.com", ClientId = "login", }.ToEntity()); context.SaveChanges(); } else { Log.Debug("OIDC IdentityProviders already populated"); } } } }