项目

OpenIddict MVC/Razor UI 迁移指南

Web 项目(非分层解决方案)

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

    <PackageReference Include="Volo.Abp.AspNetCore.Authentication.JwtBearer" Version="6.0.*" />
    <PackageReference Include="Volo.Abp.Account.Web.IdentityServer" Version="6.0.*" />
    

    替换为

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

    using Volo.Abp.AspNetCore.Authentication.JwtBearer;
    ...
    typeof(AbpAccountWebIdentityServerModule),
    typeof(AbpAspNetCoreAuthenticationJwtBearerModule),
    

    替换为

    typeof(AbpAccountWebOpenIddictModule),
    
  • MyApplicationWebModule.csConfigureServices 方法中,更新身份验证配置

    ConfigureAuthentication(context, configuration);
    

    替换为

    ConfigureAuthentication(context);
    

    并将私有的 ConfigureAuthentication 方法更新为:

    private void ConfigureAuthentication(ServiceConfigurationContext context)
    {
        context.Services.ForwardIdentityAuthenticationForBearer(OpenIddictValidationAspNetCoreDefaults.AuthenticationScheme);
    }
    
  • MyApplicationWebModule.cs 中,添加如下所示的 PreConfigureServices 方法,并将你的应用程序名称作为受众:

    public override void PreConfigureServices(ServiceConfigurationContext context)
    {
        PreConfigure<OpenIddictBuilder>(builder =>
        {
            builder.AddValidation(options =>
            {
                options.AddAudiences("MyApplication"); // 替换为你的应用程序名称
                options.UseLocalServer();
                options.UseAspNetCore();
            });
        });
    }
    
  • MyApplicationWebModule.csOnApplicationInitialization 方法中,替换 IdentityServer 和 JwtToken 中间件

    app.UseJwtTokenMiddleware();
    app.UseIdentityServer();
    

    替换为

    app.UseAbpOpenIddictValidation();
    

Web 项目(分层解决方案)

  • MyApplicationWebModule.cs 中,更新 AddAbpOpenIdConnect 配置:

    .AddAbpOpenIdConnect("oidc", options =>
    {
        options.Authority = configuration["AuthServer:Authority"];
        options.RequireHttpsMetadata = Convert.ToBoolean(configuration["AuthServer:RequireHttpsMetadata"]);
        options.ResponseType = OpenIdConnectResponseType.CodeIdToken;
    
        options.ClientId = configuration["AuthServer:ClientId"];
        options.ClientSecret = configuration["AuthServer:ClientSecret"];
    
        options.UsePkce = true; // 添加这一行
        options.SaveTokens = true;
        options.GetClaimsFromUserInfoEndpoint = true
    
        options.Scope.Add("roles"); // 将 "role" 替换为 "roles"
        options.Scope.Add("email");
        options.Scope.Add("phone");
        options.Scope.Add("MyApplication");
    });
    

    将 role 作用域替换为 roles,并添加 UsePkceSignoutScheme 选项。

IdentityServer 项目

该项目的名称在 v6.0.0 之后已重命名为 AuthServer。你也可以重构并将你的项目重命名为 AuthServer,以便将来更容易更新。

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

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

    替换为

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

    typeof(AbpAccountWebIdentityServerModule),
    

    替换为

    typeof(AbpAccountWebOpenIddictModule),
    
  • MyApplicationIdentityServerModule.cs 中,添加如下所示的 PreConfigureServices 方法,并将你的应用程序名称作为受众:

    public override void PreConfigureServices(ServiceConfigurationContext context)
    {
        PreConfigure<OpenIddictBuilder>(builder =>
        {
            builder.AddValidation(options =>
            {
                options.AddAudiences("MyApplication"); // 替换为你的应用程序名称
                options.UseLocalServer();
                options.UseAspNetCore();
            });
        });
    }
    
  • MyApplicationIdentityServerModule.csOnApplicationInitialization 方法中,移除 IdentityServer 中间件

    app.UseIdentityServer();
    
  • 要使用新的 AuthServer 页面,请将 Index.cshtml.cs 替换为 AuthServer Index.cshtml.cs,并将 Index.cshtml 文件替换为 AuthServer Index.cshtml,并将 Ids2OpenId 重命名为你的应用程序命名空间。

    注意:这些文件可以在 Pages 文件夹下找到。

Http.Api.Host 项目

  • MyApplicationHttpApiHostModule.csOnApplicationInitialization 方法中,删除 app.UseAbpSwaggerUI 选项配置中不再需要的 c.OAuthClientSecret(configuration["AuthServer:SwaggerClientSecret"]); 行。

  • appsettings.json 中,从 AuthServer 部分删除 SwaggerClientSecret,如下所示:

    "AuthServer": {
        "Authority": "https://localhost:44345",
        "RequireHttpsMetadata": "false",
        "SwaggerClientId": "MyApplication_Swagger"
    },
    

另请参阅

在本文档中