项目

从 OpenIddict 迁移到 IdentityServer4 逐步指南

自 v6.0.0 起,ABP 启动模板默认使用 OpenIddict 作为 OpenID 提供程序,并且在分层/分离解决方案中,IdentityServer 项目已重命名为 AuthServer。由于 OpenIddict 自 v6.0 起已成为 ABP 模板默认的 OpenID 提供程序库,即使 IdentityServer4已被所有者归档且不再维护,您可能仍希望继续使用它。ABP 不提供对新版本 IdentityServer 的支持。本指南为您现有的 OpenIddict 应用程序迁移到 IdentityServer4 提供了逐层的指导。

IdentityServer4 迁移步骤

使用 abp update 命令更新您现有的应用程序。更多信息请参阅升级文档。根据您的应用程序版本,按照 迁移指南 应用所需的迁移。

Domain.Shared 层

  • MyApplication.Domain.Shared.csproj 中,将项目引用
<PackageReference Include="Volo.Abp.OpenIddict.Domain.Shared" Version="6.0.*" />

替换为

<PackageReference Include="Volo.Abp.IdentityServer.Domain.Shared" Version="6.0.*" />
  • MyApplicationDomainSharedModule.cs 中,替换 using 语句和模块依赖项
using Volo.Abp.OpenIddict;
...
typeof(AbpOpenIddictDomainSharedModule)

替换为

using Volo.Abp.IdentityServer;
...
typeof(AbpIdentityServerDomainSharedModule)

Domain 层

  • MyApplication.Domain.csproj 中,将项目引用
<PackageReference Include="Volo.Abp.OpenIddict.Domain" Version="6.0.*" />
<PackageReference Include="Volo.Abp.PermissionManagement.Domain.OpenIddict" Version="6.0.*" />

替换为

<PackageReference Include="Volo.Abp.IdentityServer.Domain" Version="6.0.*" />
<PackageReference Include="Volo.Abp.PermissionManagement.Domain.IdentityServer" Version="6.0.*" />
  • MyApplicationDomainModule.cs 中,替换 using 语句和模块依赖项
using Volo.Abp.OpenIddict;
using Volo.Abp.PermissionManagement.OpenIddict;
...
typeof(AbpOpenIddictDomainModule),
typeof(AbpPermissionManagementDomainOpenIddictModule),

替换为

using Volo.Abp.IdentityServer;
using Volo.Abp.PermissionManagement.IdentityServer;
...
typeof(AbpIdentityServerDomainModule),
typeof(AbpPermissionManagementDomainIdentityServerModule),

OpenIddictDataSeedContributor

数据种子播种器是启动应用程序最重要的部分,因为它为两种 OpenID 提供程序播种初始数据。

  • 在 Domain 项目下创建一个名为 IdentityServer 的文件夹,并将 IdentityServerDataSeedContributor.cs 复制到此文件夹下。将其中所有的 OpenId2Ids 重命名为您的项目名称。
  • 删除包含不再需要的 OpenIddictDataSeedContributor.cs 文件的 OpenIddict 文件夹。

EntityFrameworkCore 层

如果您使用 MongoDB,请跳过此步骤并查看 MongoDB 层部分。

  • MyApplication.EntityFrameworkCore.csproj 中,将项目引用
<PackageReference Include="Volo.Abp.OpenIddict.EntityFrameworkCore" Version="6.0.*" />

替换为

<PackageReference Include="Volo.Abp.IdentityServer.EntityFrameworkCore" Version="6.0.*" />
  • MyApplicationEntityFrameworkCoreModule.cs 中,替换 using 语句和模块依赖项
using Volo.Abp.OpenIddict.EntityFrameworkCore;
...
typeof(AbpOpenIddictEntityFrameworkCoreModule),

替换为

using Volo.Abp.IdentityServer.EntityFrameworkCore;
...
typeof(AbpIdentityServerEntityFrameworkCoreModule),
  • MyApplicationDbContext.cs 中,替换 using 语句和Fluent API 配置
using Volo.Abp.OpenIddict.EntityFrameworkCore;
...
protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);

    /* Include modules to your migration db context */

    ...
    builder.ConfigureOpenIddict();

替换为

using Volo.Abp.IdentityServer.EntityFrameworkCore;
...
using Volo.Abp.OpenIddict.EntityFrameworkCore;
...
protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);

    /* Include modules to your migration db context */

    ...
    builder.ConfigureIdentityServer();

注意:更新 Fluent API 后,您需要创建新的数据库迁移。导航到 EntityFrameworkCore 文件夹并添加新的迁移。例如:dotnet ef migrations add Updated_To_IdentityServer

MongoDB 层

如果您使用 EntityFrameworkCore,请跳过此步骤并查看 EntityFrameworkCore 层部分。

  • MyApplication.MongoDB.csproj 中,将项目引用
<PackageReference Include="Volo.Abp.OpenIddict.MongoDB" Version="6.0.*" />

替换为

<PackageReference Include="Volo.Abp.IdentityServer.MongoDB" Version="6.0.*" />
  • MyApplicationMongoDbModule.cs 中,替换 using 语句和模块依赖项
using Volo.Abp.OpenIddict.MongoDB;
...
typeof(AbpOpenIddictMongoDbModule),

替换为

using Volo.Abp.IdentityServer.MongoDB;
...
typeof(AbpIdentityServerMongoDbModule),

DbMigrator 项目

  • appsettings.json 中,将 OpenIddict 部分替换为 IdentityServer,因为 IdentityServerDataSeeder 将使用这些信息进行初始数据播种:
"IdentityServer": { // 将 OpenIddict 重命名为 IdentityServer
    "Clients ": { // 将 Applications 重命名为 Clients
      ...
    }
  }

测试项目

  • MyApplicationTestBaseModule.csPreConfigureServices 方法中,添加 IdentityServer 相关的 using 语句和预配置,以便运行与认证相关的单元测试:
using Volo.Abp.IdentityServer;

以及

PreConfigure<AbpIdentityServerBuilderOptions>(options =>
{
    options.AddDeveloperSigningCredential = false;
});

PreConfigure<IIdentityServerBuilder>(identityServerBuilder =>
{
    identityServerBuilder.AddDeveloperSigningCredential(false, System.Guid.NewGuid().ToString());
});

UI 层

您可以按照从 IdentityServer 迁移到 OpenIddict 的指南进行反向操作,以更新您的用户界面。您也可以查看 AuthServer 项目的 Index.cshtml.csIndex.cshtml 文件的源代码。

示例和模块的源代码

在本文档中