From 1246a3e518df9b3ae793ff92abe49bfb3a693966 Mon Sep 17 00:00:00 2001 From: NachoEscrig Date: Thu, 13 Nov 2025 18:50:50 +0000 Subject: [PATCH] feat: Support custom schemas --- .../Data/ApplicationDbContext.cs | 16 ++++++++++- .../Data/IdentityDbContext.cs | 6 +++-- ...mples.OpenIddict.AdminUiIntegration.csproj | 2 +- Rsk.Samples.OpenIddict.AdminUI/Startup.cs | 27 ++++++++++++++++--- .../appsettings.json | 4 ++- 5 files changed, 46 insertions(+), 9 deletions(-) diff --git a/Rsk.Samples.OpenIddict.AdminUI/Data/ApplicationDbContext.cs b/Rsk.Samples.OpenIddict.AdminUI/Data/ApplicationDbContext.cs index 0fe9e24..b32ac01 100644 --- a/Rsk.Samples.OpenIddict.AdminUI/Data/ApplicationDbContext.cs +++ b/Rsk.Samples.OpenIddict.AdminUI/Data/ApplicationDbContext.cs @@ -1,5 +1,19 @@ +#nullable enable using Microsoft.EntityFrameworkCore; namespace Rsk.Samples.OpenIddict.AdminUiIntegration.Data; -public class ApplicationDbContext(DbContextOptions options) : DbContext(options); +public class ApplicationDbContext(DbContextOptions options, string? schema = null) : DbContext(options) +{ + private string Schema { get; } = schema ?? string.Empty; + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + if (!string.IsNullOrEmpty(Schema)) + { + modelBuilder.HasDefaultSchema(Schema); + } + + base.OnModelCreating(modelBuilder); + } +} diff --git a/Rsk.Samples.OpenIddict.AdminUI/Data/IdentityDbContext.cs b/Rsk.Samples.OpenIddict.AdminUI/Data/IdentityDbContext.cs index 16b0e3c..64331ba 100644 --- a/Rsk.Samples.OpenIddict.AdminUI/Data/IdentityDbContext.cs +++ b/Rsk.Samples.OpenIddict.AdminUI/Data/IdentityDbContext.cs @@ -1,7 +1,9 @@ +#nullable enable +using System; using IdentityExpress.Identity; using Microsoft.EntityFrameworkCore; namespace Rsk.Samples.OpenIddict.AdminUiIntegration.Data; -public class IdentityDbContext(DbContextOptions options) - : IdentityExpressDbContext(options); \ No newline at end of file +public class IdentityDbContext(DbContextOptions options, string? schema = null) + : IdentityExpressDbContext(options, schema ?? String.Empty); \ No newline at end of file diff --git a/Rsk.Samples.OpenIddict.AdminUI/Rsk.Samples.OpenIddict.AdminUiIntegration.csproj b/Rsk.Samples.OpenIddict.AdminUI/Rsk.Samples.OpenIddict.AdminUiIntegration.csproj index 529e062..cd3c68b 100644 --- a/Rsk.Samples.OpenIddict.AdminUI/Rsk.Samples.OpenIddict.AdminUiIntegration.csproj +++ b/Rsk.Samples.OpenIddict.AdminUI/Rsk.Samples.OpenIddict.AdminUiIntegration.csproj @@ -23,7 +23,7 @@ - + diff --git a/Rsk.Samples.OpenIddict.AdminUI/Startup.cs b/Rsk.Samples.OpenIddict.AdminUI/Startup.cs index 3937765..e0a36f2 100644 --- a/Rsk.Samples.OpenIddict.AdminUI/Startup.cs +++ b/Rsk.Samples.OpenIddict.AdminUI/Startup.cs @@ -31,7 +31,17 @@ public void ConfigureServices(IServiceCollection services) services.AddControllersWithViews(); services.AddRazorPages(); + var identitySchema = Configuration.GetValue("IdentityStoreSchemaName"); services.AddDbContext(GetDbConnection); + services.AddScoped(serviceProvider => + { + var options = serviceProvider + .GetRequiredService>(); + + return new IdentityDbContext(options, identitySchema); + }); + + var openIddictSchema = Configuration.GetValue("OpenIddictStoreSchemaName"); services.AddDbContext(options => { GetDbConnection(options); @@ -41,11 +51,20 @@ public void ConfigureServices(IServiceCollection services) // to replace the default OpenIddict entities. options.UseOpenIddict(); }); + services.AddScoped(serviceProvider => + { + var options = serviceProvider + .GetRequiredService>(); + + return new ApplicationDbContext(options, openIddictSchema); + }); services.AddDatabaseDeveloperPageExceptionFilter(); + // Register the Identity services. - services.AddIdentity() + services + .AddIdentity() .AddEntityFrameworkStores() .AddDefaultTokenProviders(); @@ -117,9 +136,9 @@ public void ConfigureServices(IServiceCollection services) options.AddSamlPlugin(builder => { builder.UseSamlEntityFrameworkCore() - .AddSamlArtifactDbContext(GetDbConnection) - .AddSamlConfigurationDbContext(GetDbConnection) - .AddSamlMessageDbContext(GetDbConnection); + .AddSamlArtifactDbContext(GetDbConnection, openIddictSchema) + .AddSamlConfigurationDbContext(GetDbConnection, openIddictSchema) + .AddSamlMessageDbContext(GetDbConnection, openIddictSchema); builder.ConfigureSamlOpenIddictServerOptions(serverOptions => { diff --git a/Rsk.Samples.OpenIddict.AdminUI/appsettings.json b/Rsk.Samples.OpenIddict.AdminUI/appsettings.json index 87c98e2..dc1b3a2 100644 --- a/Rsk.Samples.OpenIddict.AdminUI/appsettings.json +++ b/Rsk.Samples.OpenIddict.AdminUI/appsettings.json @@ -9,6 +9,8 @@ "AllowedHosts": "*", "DbProvider": "SqlServer", "OpenIddictConnectionString": "Server=localhost;User Id=IISConnect;Password=Password123!;Database=OpenIddictDb;Encrypt=False", + "IdentityStoreSchemaName": "", + "OpenIddictStoreSchemaName": "", "SAML2PLicensee": "DEMO", - "SAML2PLicense": "***YOUR LICENSE KEY***" + "SAML2PLicense": "" } \ No newline at end of file