项目

从 IdentityServer 迁移到 OpenIddict 逐步指南

本指南为将您现有的应用逐层迁移到 OpenIddict 提供指导。由于 OpenIddict 仅在 ABP v6.0 中可用,您需要更新现有应用才能应用 OpenIddict 变更。

历史背景

我们不会移除 Identity Server 包,并且将继续发布与 IdentityServer 相关的 NuGet/NPM 包的新版本。这意味着当稳定版发布时,您在升级到 v6.0 时不会遇到问题。我们还将继续在一段时间内修复我们包中的错误。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 的公告。

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" />
    

    用于创建宿主构建器。

  • 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 替换为您的应用程序名称。

Application Contracts 层

  • MyApplication.Application.Contracts.csproj 中,将 项目引用

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

    替换为

    <PackageReference Include="Volo.Abp.OpenIddict.Pro.Application.Contracts" Version="6.0.*" />
    
  • MyApplicationApplicationContractsModule.cs 中,替换 using 语句和 模块依赖

    using Volo.Abp.IdentityServer;
    ...
    typeof(AbpIdentityServerApplicationContractsModule),
    

    替换为

    using Volo.Abp.OpenIddict;
    ...
    typeof(AbpOpenIddictProApplicationContractsModule),
    

Application 层

  • MyApplication.Application.csproj 中,将 项目引用

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

    替换为

    <PackageReference Include="Volo.Abp.OpenIddict.Pro.Application" Version="6.0.*" />
    
  • MyApplicationApplicationModule.cs 中,替换 using 语句和 模块依赖

    using Volo.Abp.IdentityServer;
    ...
    typeof(AbpIdentityServerApplicationModule),
    

    替换为

    using Volo.Abp.OpenIddict;
    ...
    typeof(AbpOpenIddictProApplicationModule),
    

HttpApi 层

  • MyApplication.HttpApi.csproj 中,将 项目引用

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

    替换为

    <PackageReference Include="Volo.Abp.OpenIddict.Pro.HttpApi" Version="6.0.*" />
    
  • MyApplicationHttpApiModule.cs 中,替换 using 语句和 模块依赖

    using Volo.Abp.IdentityServer;
    ...
    typeof(AbpIdentityServerHttpApiModule),
    

    替换为

    using Volo.Abp.OpenIddict;
    ...
    typeof(AbpOpenIddictProHttpApiModule),
    

HttpApi.Client 层

  • MyApplication.HttpApi.Client.csproj 中,将 项目引用

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

    替换为

    <PackageReference Include="Volo.Abp.OpenIddict.Pro.HttpApi.Client" Version="6.0.*" />
    
  • MyApplicationHttpApiModule.cs 中,替换 using 语句和 模块依赖

    using Volo.Abp.IdentityServer;
    ...
    typeof(AbpIdentityServerHttpApiClientModule),
    

    替换为

    using Volo.Abp.OpenIddict;
    ...
    typeof(AbpOpenIddictProHttpApiClientModule),
    

测试项目

  • MyApplicationTestBaseModule.cs 中,移除与 IdentityServer 相关的 using 语句和 PreConfigure:

    using Volo.Abp.IdentityServer;
    

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

    PreConfigureServices 方法中移除。

数据库迁移

添加一个新的迁移,以反映底层变更: dotnet ef migrations add AbpOpenIddictMigration

UI 层

示例和模块源代码

另请参阅

在本文档中