SimplCommerce icon indicating copy to clipboard operation
SimplCommerce copied to clipboard

IdentitityServer persisted to Database

Open mrccarvalho opened this issue 5 years ago • 3 comments

Hi,

Does someone managed with IdentityServer persisted to database ? Not InMemory Configuration.

I followed the guidelines, but i have an error when I try to add migrations: dotnet ef migrations add InitialIdentityServerPersistedGrantDbMigration -c PersistedGrantDbContext -o Migrations/IdentityServer/PersistedGrantDbDb

Error: No DbContext named 'PersistedGrantDbContext' was found.

Or

No DbContext named 'ConfigurationDbContext' was found if i add dotnet ef migrations add InitialIdentityServerConfigurationDbMigration -c ConfigurationDbContext -o Migrations/IdentityServer/ConfigurationDb

Any help is appreciated.

Thanks.

mrccarvalho avatar Nov 11 '20 19:11 mrccarvalho

Hi @mrccarvalho ,

did you implement the two necessary DbContexts & DbContextOptionsBuilders, which read the respective configuration and instantiate the DbContexts?

Best regards,

Florian

fhebel avatar Nov 12 '20 10:11 fhebel

Hi @fhebel ,

first of all, thank you so much for your reply.

I have this class and now the Contexts were found.

example for ConfigurationDbContext :

public class ConfigurationDbContext : IdentityServer4.EntityFramework.DbContexts.ConfigurationDbContext<ConfigurationDbContext>
{ public ConfigurationDbContext(DbContextOptions<ConfigurationDbContext> options, ConfigurationStoreOptions storeOptions) : base(options, storeOptions) { } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (!optionsBuilder.IsConfigured) { base.OnConfiguring(optionsBuilder); } } }

on the other hand, i've got this error (after verbose migrations):

Found DbContext 'ConfigurationDbContext'. Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create an object of type 'ConfigurationDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728 ---> System.MissingMethodException: No parameterless constructor defined for type 'Clinguas.WebHost.Data.ConfigurationDbContext'.

Thank, Best regards!

mrccarvalho avatar Nov 12 '20 14:11 mrccarvalho

Hi @mrccarvalho,

sorry for my late answer.

Unfortunately it's more complicated. Keep in mind that dotnet ef migrations will actually run your code and connect to the database ...

Ok, so a good place to start is to create two context factories (sample: MigrationSimplDbContextFactory.cs):

public class ConfigurationContextFactory : IDesignTimeDbContextFactory<ConfigurationDbContext>
public class PersistedGrantContextFactory : IDesignTimeDbContextFactory<PersistedGrantDbContext>

Then you have to add your two new DbContexts to Startup.cs:

services.AddIdentityServerDataStore(_configuration);

I would suggest to implement

public static IServiceCollection AddIdentityServerDataStore(this IServiceCollection services, IConfiguration configuration)

in ServiceCollectionExtensions.cs like

public static IServiceCollection AddCustomizedDataStore(this IServiceCollection services, IConfiguration configuration)

And finally you need to initialise your IdentityServerDb in Startup.cs

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

I hope that helps ...

Best regards,

Florian

PS: You might want to go for a solution like I did for my commercial customers - that is to separate the tables of SimplCommerce from those in IdentityServer. Given that I'm using postgres I created a new schema and put the IdentityServer related tables there ...

fhebel avatar Nov 16 '20 16:11 fhebel