DuendeSoftware / Support

Support for Duende Software products
21 stars 0 forks source link

How to enumerate In Memory configuration elements #774

Closed robertellisuk closed 1 year ago

robertellisuk commented 1 year ago

I would like to know how to enumerate In Memory configuration elements in version 6 of IS.

I have found this published code example (which I think might do exactly what I want, which is to help me update my EF database config using my In Memory config as a source) but I speculate this only works in an earlier version of IS, because as far as I can see, the InMemoryConfig object does not exist in version 6?

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;
        }
    }
}

How could I modify this code to work in Identity Server 6? I'm not asking anybody to do the work for me - just some pointers as to where I should be looking in the current version of the source code would be useful.

Which version of Duende IdentityServer are you using? 6

Which version of .NET are you using? .NET 6

Describe the bug

Severity Code Description Project File Line Suppression State Error CS0103 The name 'InMemoryConfig' does not exist in the current context IdentityServer C:\Users\re-git\Repos\VerbalReasoning2\Soln\Back\IdSvr\src\IdentityServer\MigrationManager.cs 22 Active

Presumably because InMemoryConfig has been deprecated?

josephdecock commented 1 year ago

In memory clients are simply an IEnumerable<Client> that is registered in DI as a singleton. Your code can resolve that collection from DI to get the clients.

robertellisuk commented 1 year ago

Moving to Auth0 instead of IS. Thank you for your response Joseph.