added Microser

This commit is contained in:
M. Akif Tokatlioglu
2024-03-13 23:25:54 +03:00
parent d11ec09c4e
commit 7ae76afee4
208 changed files with 68884 additions and 0 deletions

View File

@@ -0,0 +1,85 @@
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<IServiceScopeFactory>().CreateScope())
{
scope.ServiceProvider.GetRequiredService<PersistedGrantDbContext>().Database.Migrate();
var context = scope.ServiceProvider.GetRequiredService<ConfigurationDbContext>();
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");
}
}
}
}