设为首页 加入收藏

TOP

第15章 使用EntityFramework Core进行配置和操作数据 - Identity Server 4 中文文档(v1.0.0)(一)
2019-09-17 18:44:26 】 浏览:42
Tags:使用 EntityFramework Core 进行 配置 操作 数据 Identity Server 中文 文档 v1.0.0

IdentityServer旨在实现可扩展性,其中一个可扩展点是用于IdentityServer所需数据的存储机制。本快速入门展示了如何配置IdentityServer以使用EntityFramework Core(EF)作为此数据的存储机制(而不是使用我们迄今为止使用的内存中实现)。

注意
除了手动配置EF支持外,还有一个IdentityServer模板可用于创建具有EF支持的新项目。使用创建它。有关更多信息,请参见此处dotnet new is4ef

15.1 IdentityServer4.EntityFramework

我们要移动到数据库有两种类型的数据:

  • 配置数据
  • 运营数据

配置数据是有关资源和客户端的配置信息。操作数据是IdentityServer在使用时生成的信息,例如令牌,代码和同意。这些存储使用接口建模,我们在IdentityServer4.EntityFramework.Storage Nuget包中提供这些接口的EF实现。

注册我们的EF实现的扩展方法包含在IdentityServer4.EntityFramework Nuget包中(引用IdentityServer4.EntityFramework.Storage)。立即从IdentityServer项目添加对IdentityServer4.EntityFramework Nuget包的引用:

cd quickstart/src/IdentityServer
dotnet add package IdentityServer4.EntityFramework

15.2 使用的SqlServer

鉴于EF的灵活性,您可以使用任何EF支持的数据库。对于本快速入门,我们将使用Visual Studio附带的SQLD的LocalDb版本。

15.3 数据库架构更改和使用EF迁移

IdentityServer4.EntityFramework.Storage包中包含从IdentityServer的模型映射实体类。作为IdentityServer模型变化,所以会在实体类IdentityServer4.EntityFramework.Storage。当您使用IdentityServer4.EntityFramework.Storage并随着时间的推移升级时,您将负责自己的数据库架构以及实体类更改时该架构所需的更改。管理这些更改的一种方法是使用EF迁移,此快速入门将显示如何完成此操作。如果迁移不是您的首选项,那么您可以以任何您认为合适的方式管理架构更改。

注意
IdentityServer4.EntityFramework.Storage中的实体维护SqlServer的最新SQL脚本。他们就在这里

15.4 配置存储

接下来的步骤是,以取代当前AddInMemoryClientsAddInMemoryIdentityResourcesAddInMemoryApiResources在在Startup.cs方法ConfigureServices。我们将使用以下代码替换它们:

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()
    .AddTestUsers(Config.GetUsers())
    // this adds the config data from DB (clients, resources)
    .AddConfigurationStore(options =>
    {
        options.ConfigureDbContext = b =>
            b.UseSqlServer(connectionString,
                sql => sql.MigrationsAssembly(migrationsAssembly));
    })
    // this adds the operational data from DB (codes, tokens, consents)
    .AddOperationalStore(options =>
    {
        options.ConfigureDbContext = b =>
            b.UseSqlServer(connectionString,
                sql => sql.MigrationsAssembly(migrationsAssembly));

        // this enables automatic token cleanup. this is optional.
        options.EnableTokenCleanup = true;
    });

您可能需要将这些命名空间添加到文件中:

using Microsoft.EntityFrameworkCore;
using System.Reflection;

上面的代码是对连接字符串进行硬编码,如果您愿意,您可以随意更改。

AddConfigurationStoreAddOperationalStore注册EF支持的存储实现。

在添加存储的调用内部,ConfigureDbContext属性的赋值注册委托以配置数据库提供程序DbContextOptionsBuilder。在这种情况下,我们调用UseSqlServer注册SQLServer。您也可以看出,这是使用连接字符串的位置。

最后,假设将使用EF迁移(至少对于此快速入门),调用将MigrationsAssembly用于通知EF将包含迁移代码的主机项目(这是必需的,因为它与包含DbContext类的程序集不同)。

我们接下来会添加迁移。

15.5 添加迁移

要创建迁移,请在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文件夹。其中包含新创建的迁移的代码。

15.6 初始化数据库

现在我们已经进行了迁移,我们可以编写代码来从

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇第7章 贡献 - Identity Server 4 .. 下一篇微服务(入门四):identityServe..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目