diff --git a/samples/Quickstarts/7_EFAndAspNetIdentity/src/IdentityServer/Data/ApplicationDbContext.cs b/samples/Quickstarts/7_EFAndAspNetIdentity/src/IdentityServer/Data/ApplicationDbContext.cs new file mode 100644 index 000000000..636e40980 --- /dev/null +++ b/samples/Quickstarts/7_EFAndAspNetIdentity/src/IdentityServer/Data/ApplicationDbContext.cs @@ -0,0 +1,21 @@ +using IdentityServerHost.Models; +using Microsoft.AspNetCore.Identity.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; + +namespace IdentityServer.Data; + +public class ApplicationDbContext : IdentityDbContext +{ + public ApplicationDbContext(DbContextOptions 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); + } +} diff --git a/samples/Quickstarts/7_EFAndAspNetIdentity/src/IdentityServer/IdentityServer.csproj b/samples/Quickstarts/7_EFAndAspNetIdentity/src/IdentityServer/IdentityServer.csproj index bb827fe79..b8154e068 100644 --- a/samples/Quickstarts/7_EFAndAspNetIdentity/src/IdentityServer/IdentityServer.csproj +++ b/samples/Quickstarts/7_EFAndAspNetIdentity/src/IdentityServer/IdentityServer.csproj @@ -6,12 +6,19 @@ + - - + + + + + + + + diff --git a/samples/Quickstarts/7_EFAndAspNetIdentity/src/IdentityServer/Models/ApplicationUser.cs b/samples/Quickstarts/7_EFAndAspNetIdentity/src/IdentityServer/Models/ApplicationUser.cs new file mode 100644 index 000000000..b54f4f8ea --- /dev/null +++ b/samples/Quickstarts/7_EFAndAspNetIdentity/src/IdentityServer/Models/ApplicationUser.cs @@ -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 + { + } +} diff --git a/samples/Quickstarts/7_EFAndAspNetIdentity/src/IdentityServer/Quickstart/Account/AccountController.cs b/samples/Quickstarts/7_EFAndAspNetIdentity/src/IdentityServer/Quickstart/Account/AccountController.cs index fb08fce49..3d426337d 100644 --- a/samples/Quickstarts/7_EFAndAspNetIdentity/src/IdentityServer/Quickstart/Account/AccountController.cs +++ b/samples/Quickstarts/7_EFAndAspNetIdentity/src/IdentityServer/Quickstart/Account/AccountController.cs @@ -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. @@ -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; diff --git a/samples/Quickstarts/7_EFAndAspNetIdentity/src/IdentityServer/Quickstart/Account/ExternalController.cs b/samples/Quickstarts/7_EFAndAspNetIdentity/src/IdentityServer/Quickstart/Account/ExternalController.cs index fe5b00381..62b2e855a 100644 --- a/samples/Quickstarts/7_EFAndAspNetIdentity/src/IdentityServer/Quickstart/Account/ExternalController.cs +++ b/samples/Quickstarts/7_EFAndAspNetIdentity/src/IdentityServer/Quickstart/Account/ExternalController.cs @@ -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; diff --git a/samples/Quickstarts/7_EFAndAspNetIdentity/src/IdentityServer/Startup.cs b/samples/Quickstarts/7_EFAndAspNetIdentity/src/IdentityServer/Startup.cs index 1b79f3c41..1a04de22c 100644 --- a/samples/Quickstarts/7_EFAndAspNetIdentity/src/IdentityServer/Startup.cs +++ b/samples/Quickstarts/7_EFAndAspNetIdentity/src/IdentityServer/Startup.cs @@ -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 { @@ -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(options => + { + options.UseSqlServer(connectionString); + }); + + services.AddIdentity() + .AddEntityFrameworkStores() + .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, @@ -37,8 +56,10 @@ public void ConfigureServices(IServiceCollection services) { options.ConfigureDbContext = b => b.UseSqlServer(connectionString, sql => sql.MigrationsAssembly(migrationsAssembly)); - }); + }) + .AddAspNetIdentity(); + // not recommended for production - you need to store your key material somewhere secure builder.AddDeveloperSigningCredential(); services.AddAuthentication() @@ -76,6 +97,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); + app.UseDatabaseErrorPage(); } app.UseStaticFiles(); @@ -94,6 +116,8 @@ private void InitializeDatabase(IApplicationBuilder app) { using (var serviceScope = app.ApplicationServices.GetService().CreateScope()) { + serviceScope.ServiceProvider.GetRequiredService().Database.Migrate(); + serviceScope.ServiceProvider.GetRequiredService().Database.Migrate(); var context = serviceScope.ServiceProvider.GetRequiredService();