Skip to content

Commit

Permalink
up and running with EF/AspnetIdentity quick-start.
Browse files Browse the repository at this point in the history
  • Loading branch information
dementeddevil committed Nov 12, 2024
1 parent b4a67da commit d3b6ed4
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using IdentityServerHost.Models;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace IdentityServer.Data;

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}

protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@

<ItemGroup>
<PackageReference Include="Zen.IdentityServer.EntityFramework" />
<PackageReference Include="Zen.IdentityServer.AspNetIdentity" />

<PackageReference Include="Serilog.AspNetCore" />

<PackageReference Include="Microsoft.AspNetCore.Authentication.Google" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" />

<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.2.0" />

<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="8.0.10" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.10" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="8.0.10" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Microsoft.AspNetCore.Identity;

namespace IdentityServerHost.Models
{
// Add profile data for application users by adding properties to the ApplicationUser class
public class ApplicationUser : IdentityUser
{
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.


Expand All @@ -8,7 +8,7 @@
using Zen.IdentityServer.Models;
using Zen.IdentityServer.Services;
using Zen.IdentityServer.Stores;
using IdentityServerAspNetIdentity.Models;
using IdentityServerHost.Models;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using Zen.IdentityServer.Events;
using Zen.IdentityServer.Services;
using Zen.IdentityServer.Stores;
using IdentityServerAspNetIdentity.Models;
using IdentityServerHost.Models;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.


using Zen.IdentityServer;
using Zen.IdentityServer.EntityFramework.DbContexts;
using Zen.IdentityServer.EntityFramework.Mappers;
using IdentityServerHost.Quickstart.UI;
using System.Linq;
using System.Reflection;
using IdentityServer.Data;
using IdentityServerHost.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Tokens;
using System.Linq;
using System.Reflection;
using Zen.IdentityServer;
using Zen.IdentityServer.EntityFramework.DbContexts;
using Zen.IdentityServer.EntityFramework.Mappers;

namespace IdentityServer
{
Expand All @@ -24,10 +26,27 @@ public void ConfigureServices(IServiceCollection services)
services.AddControllersWithViews();

var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;
const string connectionString = @"Data Source=(LocalDb)\MSSQLLocalDB;database=Zen.IdentityServer.Quickstart.EntityFramework-4.0.0;trusted_connection=yes;";

var builder = services.AddIdentityServer()
.AddTestUsers(TestUsers.Users)
const string connectionString = @"Data Source=(LocalDb)\MSSQLLocalDB;database=Zen.IdentityServer.Quickstart.EFAndAspNetIdentity;trusted_connection=yes;";

services.AddDbContext<ApplicationDbContext>(options =>
{
options.UseSqlServer(connectionString);
});

services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();

var builder = services
.AddIdentityServer(options =>
{
options.Events.RaiseErrorEvents = true;
options.Events.RaiseInformationEvents = true;
options.Events.RaiseFailureEvents = true;
options.Events.RaiseSuccessEvents = true;

options.EmitStaticAudienceClaim = true;
})
.AddConfigurationStore(options =>
{
options.ConfigureDbContext = b => b.UseSqlServer(connectionString,
Expand All @@ -37,8 +56,10 @@ public void ConfigureServices(IServiceCollection services)
{
options.ConfigureDbContext = b => b.UseSqlServer(connectionString,
sql => sql.MigrationsAssembly(migrationsAssembly));
});
})
.AddAspNetIdentity<ApplicationUser>();

// not recommended for production - you need to store your key material somewhere secure
builder.AddDeveloperSigningCredential();

services.AddAuthentication()
Expand Down Expand Up @@ -76,6 +97,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}

app.UseStaticFiles();
Expand All @@ -94,6 +116,8 @@ private void InitializeDatabase(IApplicationBuilder app)
{
using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
{
serviceScope.ServiceProvider.GetRequiredService<ApplicationDbContext>().Database.Migrate();

serviceScope.ServiceProvider.GetRequiredService<PersistedGrantDbContext>().Database.Migrate();

var context = serviceScope.ServiceProvider.GetRequiredService<ConfigurationDbContext>();
Expand Down

0 comments on commit d3b6ed4

Please sign in to comment.