寫在最前面,此文章不詳細講述IdentityServer4的各個組成部分以及Oauth2相關知識,須要瞭解詳細相關知識的,能夠移步個人其餘幾篇博客(初探IdentityServer4),騰訊視頻有Dave老師錄製的詳細教程(http://v.qq.com/vplus/4cfb00af75c16eb8d198c58fb86eb4dc?page=video)。git
1 public class Startup 2 { 3 public IConfiguration Configuration { get; } 4 public IHostingEnvironment Environment { get; } 5 public Startup(IConfiguration configuration, IHostingEnvironment environment) 6 { 7 Configuration = configuration; 8 Environment = environment; 9 } 10 public void ConfigureServices(IServiceCollection services) 11 { 12 services.AddMvcCore() 13 .AddAuthorization() 14 .AddJsonFormatters(); 15 16 services.AddMvc().SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Version_2_1); 17 18 19 20 services.Configure<IISOptions>(iis => 21 { 22 iis.AuthenticationDisplayName = "Windows"; 23 iis.AutomaticAuthentication = false; 24 }); 25 26 27 var builder = services.AddIdentityServer(options => 28 { 29 options.Events.RaiseErrorEvents = true; 30 options.Events.RaiseInformationEvents = true; 31 options.Events.RaiseFailureEvents = true; 32 options.Events.RaiseSuccessEvents = true; 33 }) 34 .AddInMemoryIdentityResources(Config.GetIdentityResources()) 35 .AddInMemoryApiResources(Config.GetApis()) 36 .AddInMemoryClients(Config.GetClients()) 37 .AddTestUsers(Config.GetUsers()); 38 39 if (Environment.IsDevelopment()) 40 { 41 builder.AddDeveloperSigningCredential(); 42 } 43 else 44 { 45 throw new Exception("need to configure key material"); 46 } 47 } 48 public void Configure(IApplicationBuilder app, IHostingEnvironment env) 49 { 50 if (Environment.IsDevelopment()) 51 { 52 app.UseDeveloperExceptionPage(); 53 app.UseDatabaseErrorPage(); 54 } 55 else 56 { 57 app.UseExceptionHandler("/Home/Error"); 58 } 59 app.UseStaticFiles(); 60 app.UseMvcWithDefaultRoute(); 61 } 62 63 } 64 }
1 public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 2 { 3 public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } 4 protected override void OnModelCreating(ModelBuilder builder) 5 { 6 base.OnModelCreating(builder); 7 } 8 }
1 { 2 "ConnectionStrings": { 3 "DefaultConnection": "Data Source=identity.db;" 4 } 5 }
最後,添加Identity的數據庫遷移文件,更新數據庫github
1 Add-Migration CreateIdentitySchema 2 3 Update-Database
1 private void InitializeDatabase(IApplicationBuilder app) 2 { 3 using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope()) 4 { 5 serviceScope.ServiceProvider.GetRequiredService<PersistedGrantDbContext>().Database.Migrate(); 6 7 var context = serviceScope.ServiceProvider.GetRequiredService<ConfigurationDbContext>(); 8 context.Database.Migrate(); 9 if (!context.Clients.Any()) 10 { 11 foreach (var client in Config.GetClients()) 12 { 13 context.Clients.Add(client.ToEntity()); 14 } 15 context.SaveChanges(); 16 } 17 if (!context.IdentityResources.Any()) 18 { 19 foreach (var resource in Config.GetIdentityResources()) 20 { 21 context.IdentityResources.Add(resource.ToEntity()); 22 } 23 context.SaveChanges(); 24 } 25 if (!context.ApiResources.Any()) 26 { 27 foreach (var resource in Config.GetApis()) 28 { 29 context.ApiResources.Add(resource.ToEntity()); 30 } 31 context.SaveChanges(); 32 } 33 } 34 }
1 private void InitializeDatabase(IApplicationBuilder app) 2 { 3 using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope()) 4 { 5 serviceScope.ServiceProvider.GetRequiredService<PersistedGrantDbContext>().Database.Migrate(); 6 7 var context = serviceScope.ServiceProvider.GetRequiredService<ConfigurationDbContext>(); 8 context.Database.Migrate(); 9 if (!context.Clients.Any()) 10 { 11 foreach (var client in Config.GetClients()) 12 { 13 context.Clients.Add(client.ToEntity()); 14 } 15 context.SaveChanges(); 16 } 17 if (!context.IdentityResources.Any()) 18 { 19 foreach (var resource in Config.GetIdentityResources()) 20 { 21 context.IdentityResources.Add(resource.ToEntity()); 22 } 23 context.SaveChanges(); 24 } 25 if (!context.ApiResources.Any()) 26 { 27 foreach (var resource in Config.GetApis()) 28 { 29 context.ApiResources.Add(resource.ToEntity()); 30 } 31 context.SaveChanges(); 32 } 33 } 34 }