项目

OpenIddict Blazor-Server UI 迁移指南

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

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

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

    替换为

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

    using Volo.Abp.AspNetCore.Authentication.JwtBearer;
    using Volo.Abp.IdentityServer.Blazor.Server;
    ...
    typeof(AbpAccountPublicWebIdentityServerModule),
    typeof(AbpIdentityServerBlazorServerModule),
    

    替换为

    using OpenIddict.Validation.AspNetCore;
    using Volo.Abp.OpenIddict.Pro.Blazor.Server;
    ...
    typeof(AbpAccountPublicWebOpenIddictModule),
    typeof(AbpOpenIddictProBlazorServerModule),
    
  • MyApplicationBlazorModule.cs 中添加如下所示的 PreConfigureServices,并将您的应用程序名称作为受众:

    public override void PreConfigureServices(ServiceConfigurationContext context)
    {
        PreConfigure<OpenIddictBuilder>(builder =>
        {
            builder.AddValidation(options =>
            {
                options.AddAudiences("MyApplication"); // 替换为您的应用程序名称
                options.UseLocalServer();
                options.UseAspNetCore();
            });
        });
    }
    
  • MyApplicationBlazorModule.csConfigureServices 方法中,替换方法调用

    ConfigureAuthentication(context, configuration); 替换为 ConfigureAuthentication(context); 并更新方法如下:

    private void ConfigureAuthentication(ServiceConfigurationContext context)
    {
        context.Services.ForwardIdentityAuthenticationForBearer(OpenIddictValidationAspNetCoreDefaults.AuthenticationScheme);
    }
    
  • MyApplicationBlazorModule.csOnApplicationInitialization 方法中,替换中间件

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

    替换为

    app.UseAbpOpenIddictValidation();
    
  • Menus 文件夹下的 MyApplicationMenuContributor.cs 文件中,于 ConfigureMainMenuAsync 方法下替换 using 语句和菜单名称

    using Volo.Abp.IdentityServer.Blazor.Navigation;
    ...
    //Administration->Identity Server
    administration.SetSubItemOrder(AbpIdentityServerMenuNames.GroupName, 2);
    

    替换为

    using Volo.Abp.OpenIddict.Pro.Blazor.Menus;
    ...
    //Administration->OpenIddict
    administration.SetSubItemOrder(OpenIddictProMenus.GroupName, 2);
    

Blazor 项目 (分层解决方案)

  • 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.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

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"
    },
    

IdentityServer

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

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

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

    替换为

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

    using IdentityServer4.Configuration;
    using Volo.Abp.AspNetCore.Authentication.JwtBearer;
    ...
    typeof(AbpAccountPublicWebIdentityServerModule),
    

    替换为

    using OpenIddict.Validation.AspNetCore;
    ...
    typeof(AbpAccountPublicWebOpenIddictModule),
    
  • MyApplicationIdentityServerModule.cs 中添加如下所示的 PreConfigureServices,并将您的应用程序名称作为受众:

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

    context.Services.ForwardIdentityAuthenticationForBearer();
    

    替换为

    context.Services.ForwardIdentityAuthenticationForBearer(OpenIddictValidationAspNetCoreDefaults.AuthenticationScheme);
    
  • MyApplicationIdentityServerModule.csConfigureServices 方法中,移除 IdentityServerOptions 配置和 JwtBearer 选项

    if (Convert.ToBoolean(configuration["AuthServer:SetSelfAsIssuer"])) // 移除
    {
        Configure<IdentityServerOptions>(options => { options.IssuerUri = configuration["App:SelfUrl"]; });
    }
    ...
    context.Services.AddAuthentication() // 移除
        .AddJwtBearer(options =>
        {
           options.Authority = configuration["AuthServer:Authority"];
           options.RequireHttpsMetadata = Convert.ToBoolean(configuration["AuthServer:RequireHttpsMetadata"]);
           options.Audience = "MyApplication";
        })
    
  • MyApplicationIdentityServerModule.csOnApplicationInitialization 方法中,替换中间件

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

    替换为

    app.UseAbpOpenIddictValidation();
    
  • 要使用新的 AuthServer 页面,请将 Index.cshtml.cs 替换为 AuthServer Index.cshtml.cs 以及将 Index.cshtml 文件替换为 AuthServer Index.cshtml

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

另请参阅

在本文档中