-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathSeedData.cs
More file actions
99 lines (89 loc) · 3.25 KB
/
SeedData.cs
File metadata and controls
99 lines (89 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) Duende Software. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using Duende.IdentityServer.EntityFramework.DbContexts;
using Duende.IdentityServer.EntityFramework.Mappers;
using Duende.IdentityServer.EntityFramework.Storage;
using Duende.IdentityServer.Models;
using Microsoft.EntityFrameworkCore;
namespace IdentityServerHost;
public class SeedData
{
public static void EnsureSeedData(string connectionString)
{
var services = new ServiceCollection();
services.AddOperationalDbContext(options =>
{
options.ConfigureDbContext = db => db.UseSqlite(connectionString, sql => sql.MigrationsAssembly(typeof(SeedData).Assembly.FullName));
});
services.AddConfigurationDbContext(options =>
{
options.ConfigureDbContext = db => db.UseSqlite(connectionString, sql => sql.MigrationsAssembly(typeof(SeedData).Assembly.FullName));
});
var serviceProvider = services.BuildServiceProvider();
using (var scope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
scope.ServiceProvider.GetService<PersistedGrantDbContext>().Database.Migrate();
var context = scope.ServiceProvider.GetService<ConfigurationDbContext>();
context.Database.Migrate();
EnsureSeedData(context);
}
}
private static void EnsureSeedData(ConfigurationDbContext context)
{
if (!context.Clients.Any())
{
Console.WriteLine("Clients being populated");
foreach (var client in Config.Clients.ToList())
{
context.Clients.Add(client.ToEntity());
}
context.SaveChanges();
}
else
{
Console.WriteLine("Clients already populated");
}
if (!context.IdentityResources.Any())
{
Console.WriteLine("IdentityResources being populated");
foreach (var resource in Config.IdentityResources.ToList())
{
context.IdentityResources.Add(resource.ToEntity());
}
context.SaveChanges();
}
else
{
Console.WriteLine("IdentityResources already populated");
}
if (!context.ApiScopes.Any())
{
Console.WriteLine("ApiScopes being populated");
foreach (var apiScope in Config.ApiScopes.ToList())
{
context.ApiScopes.Add(apiScope.ToEntity());
}
context.SaveChanges();
}
else
{
Console.WriteLine("ApiScopes already populated");
}
if (!context.IdentityProviders.Any())
{
Console.WriteLine("IdentityProviders being populated");
context.IdentityProviders.Add(new OidcProvider
{
Scheme = "demoidsrv",
DisplayName = "IdentityServer (dynamic)",
Authority = "https://demo.duendesoftware.com",
ClientId = "login",
}.ToEntity());
context.SaveChanges();
}
else
{
Console.WriteLine("OidcIdentityProviders already populated");
}
}
}