Skip to content

Commit

Permalink
Using Entity Framework Core storage of configuration data
Browse files Browse the repository at this point in the history
  • Loading branch information
elanderson authored Jun 26, 2017
1 parent 16c3951 commit a09f200
Show file tree
Hide file tree
Showing 9 changed files with 1,794 additions and 9 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using IdentityServer4.EntityFramework.DbContexts;
using IdentityServer4.EntityFramework.Mappers;
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System.Linq;

namespace IdentityApp.Data.Migrations.IdentityServer
{
public static class IdentityServerDatabaseInitialization
{
public static void InitializeDatabase(IApplicationBuilder app)
{
using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
{
PerformMigrations(serviceScope);
SeedData(serviceScope);
}
}

private static void PerformMigrations(IServiceScope serviceScope)
{
serviceScope.ServiceProvider.GetRequiredService<ConfigurationDbContext>().Database.Migrate();
serviceScope.ServiceProvider.GetRequiredService<PersistedGrantDbContext>().Database.Migrate();
}

private static void SeedData(IServiceScope serviceScope)
{
var context = serviceScope.ServiceProvider.GetRequiredService<ConfigurationDbContext>();

if (!context.Clients.Any())
{
foreach (var client in Config.GetClients())
{
context.Clients.Add(client.ToEntity());
}
context.SaveChanges();
}

if (!context.IdentityResources.Any())
{
foreach (var resource in Config.GetIdentityResources())
{
context.IdentityResources.Add(resource.ToEntity());
}
context.SaveChanges();
}

if (!context.ApiResources.Any())
{
foreach (var resource in Config.GetApiResources())
{
context.ApiResources.Add(resource.ToEntity());
}
context.SaveChanges();
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore.Migrations;

namespace IdentityApp.Data.Migrations.IdentityServer.PersistedGrant
{
public partial class InitPersistedGrant : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "PersistedGrants",
columns: table => new
{
Key = table.Column<string>(maxLength: 200, nullable: false),
ClientId = table.Column<string>(maxLength: 200, nullable: false),
CreationTime = table.Column<DateTime>(nullable: false),
Data = table.Column<string>(maxLength: 50000, nullable: false),
Expiration = table.Column<DateTime>(nullable: true),
SubjectId = table.Column<string>(maxLength: 200, nullable: true),
Type = table.Column<string>(maxLength: 50, nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_PersistedGrants", x => x.Key);
});

migrationBuilder.CreateIndex(
name: "IX_PersistedGrants_SubjectId_ClientId_Type",
table: "PersistedGrants",
columns: new[] { "SubjectId", "ClientId", "Type" });
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "PersistedGrants");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using IdentityServer4.EntityFramework.DbContexts;

namespace IdentityApp.Data.Migrations.IdentityServer.PersistedGrant
{
[DbContext(typeof(PersistedGrantDbContext))]
partial class PersistedGrantDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
modelBuilder
.HasAnnotation("ProductVersion", "1.1.2")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);

modelBuilder.Entity("IdentityServer4.EntityFramework.Entities.PersistedGrant", b =>
{
b.Property<string>("Key")
.HasMaxLength(200);

b.Property<string>("ClientId")
.IsRequired()
.HasMaxLength(200);

b.Property<DateTime>("CreationTime");

b.Property<string>("Data")
.IsRequired()
.HasMaxLength(50000);

b.Property<DateTime?>("Expiration");

b.Property<string>("SubjectId")
.HasMaxLength(200);

b.Property<string>("Type")
.IsRequired()
.HasMaxLength(50);

b.HasKey("Key");

b.HasIndex("SubjectId", "ClientId", "Type");

b.ToTable("PersistedGrants");
});
}
}
}
1 change: 1 addition & 0 deletions IdentityApp/IdentityApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="IdentityServer4.AspNetIdentity" Version="1.0.1" />
<PackageReference Include="IdentityServer4.EntityFramework" Version="1.0.1" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.1" />
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="1.1.2" />
Expand Down
22 changes: 13 additions & 9 deletions IdentityApp/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
Expand All @@ -10,6 +7,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using IdentityApp.Data;
using IdentityApp.Data.Migrations.IdentityServer;
using IdentityApp.Models;
using IdentityApp.Services;

Expand Down Expand Up @@ -53,13 +51,17 @@ public void ConfigureServices(IServiceCollection services)
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();

var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;

services.AddIdentityServer()
.AddTemporarySigningCredential()
.AddInMemoryPersistedGrants()
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients())
.AddAspNetIdentity<ApplicationUser>();
.AddAspNetIdentity<ApplicationUser>()
.AddConfigurationStore(builder =>
builder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"), options =>
options.MigrationsAssembly(migrationsAssembly)))
.AddOperationalStore(builder =>
builder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"), options =>
options.MigrationsAssembly(migrationsAssembly)));
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand All @@ -79,6 +81,8 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
app.UseExceptionHandler("/Home/Error");
}

IdentityServerDatabaseInitialization.InitializeDatabase(app);

app.UseStaticFiles();

app.UseIdentity();
Expand Down

0 comments on commit a09f200

Please sign in to comment.