I'm using Identity Server 6 and have followed various quickstarts to get started with this. I have a coded configuration (in my Config.cs file) but my IS server is now using the EF Database configuration provider for MS SQL. I now want to take my coded client configuration and migrate it in to my database. I found an article which outlines one approach to writing code that will do this. The code is shown below. However, this code won't compile in my IS 6 project, because the InMemoryConfig object is not recognised. I'm speculating that this is because the code was perhaps built for an earlier version of IS; however since I am not familiar with the IS source code, I don't know where to even begin when it comes to updating or adapting this.
Basically, any solution which will automate the process of getting the IS 6 code configuration written in to the IS 6 EF database, is what I'm looking for.
Would very much appreciate any pointers in the right direction. Thanks in advance.
using Duende.IdentityServer.EntityFramework.DbContexts;
using Microsoft.EntityFrameworkCore;
namespace IdentityServer
{
public static class MigrationManager
{
public static IHost MigrateDatabase(this IHost host)
{
using (var scope = host.Services.CreateScope())
{
scope.ServiceProvider.GetRequiredService<PersistedGrantDbContext>().Database.Migrate();
using (var context = scope.ServiceProvider.GetRequiredService<ConfigurationDbContext>())
{
try
{
context.Database.Migrate();
if (!context.Clients.Any())
{
foreach (var client in InMemoryConfig.GetClients())
{
context.Clients.Add(client.ToEntity());
}
context.SaveChanges();
}
if (!context.IdentityResources.Any())
{
foreach (var resource in InMemoryConfig.GetIdentityResources())
{
context.IdentityResources.Add(resource.ToEntity());
}
context.SaveChanges();
}
if (!context.ApiScopes.Any())
{
foreach (var apiScope in InMemoryConfig.GetApiScopes())
{
context.ApiScopes.Add(apiScope.ToEntity());
}
context.SaveChanges();
}
if (!context.ApiResources.Any())
{
foreach (var resource in InMemoryConfig.GetApiResources())
{
context.ApiResources.Add(resource.ToEntity());
}
context.SaveChanges();
}
}
catch (Exception ex)
{
//Log errors or do anything you think it's needed
throw;
}
}
}
return host;
}
}
}