项目

从 IdentityServer 迁移到 OpenIddict 分步指南

本指南为将您现有的应用程序从 IdentityServer 迁移到 OpenIddict 提供逐层指导。ABP 启动模板从 v6.0.0 开始默认使用 OpenIddict 作为 OpenId 提供程序,并且在分层/分离解决方案中,IdentityServer 项目已重命名为 AuthServer。由于 OpenIddict 仅在 ABP v6.0 中可用,您需要更新现有应用程序才能应用 OpenIddict 的更改。

历史背景

我们并未移除 Identity Server 包,并且将继续发布 IdentityServer 相关 NuGet/NPM 包的新版本。这意味着当稳定版发布时,升级到 v6.0 不会遇到问题。我们还将继续修复我们包中的 bug 一段时间。ABP 7.0 将基于 .NET 7。如果 Identity Server 继续兼容 .NET 7,我们也将继续为我们 IDS 集成发布 NuGet 包。

另一方面,Identity Server 在 2022 年底停止了对开源 Identity Server 的支持。Identity Server 团队已决定转向 Duende IDS,而 ABP 将不会迁移到商业版的 Duende IDS。您可以通过此链接查看 Duende Identity Server 的公告。

商业模板

如果您使用的是商业模板,请查看为商业模板从 IdentityServer 迁移到 OpenIddict指南。 如果您使用的是微服务模板,请查看将微服务模板从 IdentityServer 迁移到 OpenIddict指南。

OpenIddict 迁移步骤

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

Domain.Shared 层

  • MyApplication.Domain.Shared.csproj 中替换项目引用
<PackageReference Include="Volo.Abp.IdentityServer.Domain.Shared" Version="6.0.*" />

替换为

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

替换为

using Volo.Abp.OpenIddict;
...
typeof(AbpOpenIddictDomainSharedModule)

Domain 层

  • MyApplication.Domain.csproj 中替换项目引用
<PackageReference Include="Volo.Abp.IdentityServer.Domain" Version="6.0.*" />
<PackageReference Include="Volo.Abp.PermissionManagement.Domain.IdentityServer" Version="6.0.*" />

替换为

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

替换为

using Volo.Abp.OpenIddict;
using Volo.Abp.PermissionManagement.OpenIddict;
...
typeof(AbpOpenIddictDomainModule),
typeof(AbpPermissionManagementDomainOpenIddictModule),

OpenIddictDataSeedContributor

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

您也可以创建一个同名的新项目,并将其 OpenIddict 文件夹复制到您的项目中。

EntityFrameworkCore 层

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

  • MyApplication.EntityFrameworkCore.csproj 中替换项目引用

    <PackageReference Include="Volo.Abp.IdentityServer.EntityFrameworkCore" Version="6.0.*" />
    

    替换为

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

using Volo.Abp.IdentityServer.EntityFrameworkCore;
...
typeof(AbpIdentityServerEntityFrameworkCoreModule),

替换为

using Volo.Abp.OpenIddict.EntityFrameworkCore;
...
typeof(AbpOpenIddictEntityFrameworkCoreModule),
  • MyApplicationDbContext.cs 中替换 using 语句和Fluent API 配置

    using Volo.Abp.IdentityServer.EntityFrameworkCore;
    ...
    using Volo.Abp.OpenIddict.EntityFrameworkCore;
    ...
    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
    
        /* 将模块包含到您的迁移数据库上下文中 */
    
        ...
        builder.ConfigureIdentityServer();
    

    替换为

    using Volo.Abp.OpenIddict.EntityFrameworkCore;
    ...
    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
    
        /* 将模块包含到您的迁移数据库上下文中 */
    
        ...
        builder.ConfigureOpenIddict();
    

MongoDB 层

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

  • MyApplication.MongoDB.csproj 中替换项目引用

    <PackageReference Include="Volo.Abp.IdentityServer.MongoDB" Version="6.0.*" />
    

    替换为

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

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

替换为

using Volo.Abp.OpenIddict.MongoDB;
...
typeof(AbpOpenIddictMongoDbModule),

DbMigrator 项目

  • MyApplication.DbMigrator.csproj添加项目引用

    <PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
    

用于创建 Host Builder。

  • appsettings.json将 IdentityServer 部分替换为 OpenIddict:

    "OpenIddict": {
        "Applications": {
          "MyApplication_Web": {
            "ClientId": "MyApplication_Web",
            "ClientSecret": "1q2w3e*",
            "RootUrl": "https://localhost:44384"
          },
          "MyApplication_App": {
            "ClientId": "MyApplication_App",
            "RootUrl": "http://localhost:4200"
          },
          "MyApplication_BlazorServerTiered": {
            "ClientId": "MyApplication_BlazorServerTiered",
            "ClientSecret": "1q2w3e*",
            "RootUrl": "https://localhost:44346"
          },
          "MyApplication_Swagger": {
            "ClientId": "MyApplication_Swagger",
            "RootUrl": "https://localhost:44391"
          }
        }
      }
    

    MyApplication 替换为您的应用程序名称。

测试项目

  • MyApplicationTestBaseModule.cs移除 IdentityServer 相关的 using 和预配置:

    using Volo.Abp.IdentityServer;
    

    以及

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

    PreConfigureServices 中移除。

UI 层

示例和模块源代码

另请参阅

在本文档中