本文用了EF,若是不适用EF的,请参考这篇文章,实现这些接口来本身定义存储等逻辑。http://www.cnblogs.com/stulzq/p/8144056.htmlhtml
IdentityServer具备良好的扩展性,其中一个可扩展点是用于IdentityServer所需数据的存储机制。 本快速入门介绍了如何配置IdentityServer以使用EntityFramework(EF)做为此数据的存储机制(而不是使用咱们迄今为止使用的内存中实现)。git
有两种类型的数据须要持久化到数据库中。 首先是配置数据(资源和客户端),第二个是IdentityServer在使用时产生的操做数据(令牌,代码和用户的受权信息consents)。 这些存储采用接口进行建模,咱们在IdentityServer4.EntityFramework
Nuget包中提供这些接口的EF实现。github
IdentityServer项目经过添加对IdentityServer4.EntityFramework
Nuget包的引用开始。sql
鉴于EF的灵活性,您可使用任何EF支持的数据库。 对于这个快速入门,咱们将使用Visual Studio附带的SqlServer的LocalDb版本。shell
IdentityServer4.EntityFramework
包包含从IdentityServer的模型映射的实体类。 随着IdentityServer的模型的改变,IdentityServer4.EntityFramework
中的实体类也会改变。 当您使用IdentityServer4.EntityFramework
并随着时间的推移升级时,您将负责本身的数据库Schema以及实体类更改所需的更改。 管理这些变化的一种方法是使用EF迁移,这个快速入门将显示如何完成。 若是迁移不是您的偏好,那么您能够以任何您认为合适的方式管理架构更改。数据库
为IdentityServer4.EntityFramework中的实体维护SqlServer的SQL脚本。 https://github.com/IdentityServer/IdentityServer4.EntityFramework/tree/dev/src/Host/Migrations/IdentityServer架构
关于EF迁移能够看个人这篇文章:http://www.cnblogs.com/stulzq/p/7717873.htmlapp
咱们须要手动更改项目的csproj文件来添加EF工具:ide
而后在结束</ Project>元素以前添加下面的代码片断:函数
<ItemGroup> <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" /> </ItemGroup>
看起来像这样:
保存并关闭文件。 为了测试你已经正确安装了这些工具,你能够在项目所在的目录下打开一个命令shell并运行dotnet ef。 它应该是这样的:
下一步是在Startup.cs中ConfigureServices方法中的AddInMemoryClients,AddInMemoryIdentityResources和AddInMemoryApiResources进行替换。 咱们将用这个代码替换它们:
const string connectionString = @"Data Source=(LocalDb)\MSSQLLocalDB;database=IdentityServer4.Quickstart.EntityFramework-2.0.0;trusted_connection=yes;"; var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name; // configure identity server with in-memory stores, keys, clients and scopes services.AddIdentityServer() .AddDeveloperSigningCredential() .AddTestUsers(Config.GetUsers()) // this adds the config data from DB (clients, resources) .AddConfigurationStore(options => { options.ConfigureDbContext = builder => builder.UseSqlServer(connectionString, sql => sql.MigrationsAssembly(migrationsAssembly)); }) // this adds the operational data from DB (codes, tokens, consents) .AddOperationalStore(options => { options.ConfigureDbContext = builder => builder.UseSqlServer(connectionString, sql => sql.MigrationsAssembly(migrationsAssembly)); // this enables automatic token cleanup. this is optional. options.EnableTokenCleanup = true; options.TokenCleanupInterval = 30; });
您可能须要将这些命名空间添加到文件中:
using Microsoft.EntityFrameworkCore; using System.Reflection;
上面的代码是对一个链接字符串进行硬编码,若是你愿意,你能够随意更改。 此外,对AddConfigurationStore
和AddOperationalStore
的调用是注册EF支持的存储实现。
传递给这些API的“builder”回调方法是EF的机制,容许您为这两个存储中的每个配置用于DbContext
的DbContextOptionsBuilder
。 这就是咱们的DbContext类能够用你想要使用的数据库提供程序来配置。 在这种状况下,经过调用UseSqlServer
,咱们正在使用SqlServer。 你也能够知道,这是提供链接字符串的地方。
UseSqlServer中的“options”回调函数是配置定义EF迁移的程序集的方法。 EF须要使用迁移来定义数据库的Schema。
要建立迁移,请在IdentityServer项目目录中打开命令提示符。 在命令提示符下运行这两个命令:
dotnet ef migrations add InitialIdentityServerPersistedGrantDbMigration -c PersistedGrantDbContext -o Data/Migrations/IdentityServer/PersistedGrantDb dotnet ef migrations add InitialIdentityServerConfigurationDbMigration -c ConfigurationDbContext -o Data/Migrations/IdentityServer/ConfigurationDb
执行状况应该以下:
您如今应该在项目中看到一个〜/ Data / Migrations / IdentityServer
文件夹。 这包含新建立的迁移的代码。
如今咱们已经添加了迁移,咱们能够编写代码来从迁移中建立数据库。 咱们还将使用咱们在以前的快速入门中定义的内存配置数据对数据库进行种子处理。
在Startup.cs中添加这个方法来帮助初始化数据库:
private void InitializeDatabase(IApplicationBuilder app) { using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope()) { serviceScope.ServiceProvider.GetRequiredService<PersistedGrantDbContext>().Database.Migrate(); var context = serviceScope.ServiceProvider.GetRequiredService<ConfigurationDbContext>(); context.Database.Migrate(); 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(); } } }
而后咱们能够从Configure方法调用它:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { // this will do the initial DB population InitializeDatabase(app); // the rest of the code that was already here // ... }
如今,若是运行IdentityServer项目,则应建立数据库并使用快速入门配置数据进行种子插入。 您应该可以使用SQL Server Management Studio或Visual Studio来链接和检查数据。
您如今应该可以运行任何现有的客户端应用程序并登陆,获取令牌并调用API - 所有基于数据库配置。
本文代码:https://github.com/IdentityServer/IdentityServer4.Samples/tree/master/Quickstarts/7_EntityFrameworkStorage 原文:https://identityserver4.readthedocs.io/en/latest/quickstarts/7_entity_framework.html