OpenIddict MVC/Razor UI 迁移指南
Web 项目 (非分层解决方案)
在 MyApplication.Web.csproj 中替换 项目引用:
<PackageReference Include="Volo.Abp.Account.Pro.Public.Web.IdentityServer" Version="6.0.*" /> <PackageReference Include="Volo.Abp.IdentityServer.Web" Version="6.0.*" />替换为
<PackageReference Include="Volo.Abp.Account.Pro.Public.Web.OpenIddict" Version="6.0.*" /> <PackageReference Include="Volo.Abp.OpenIddict.Pro.Web" Version="6.0.*" />在 MyApplicationWebModule.cs 中替换 using 语句和 模块依赖项:
using Volo.Abp.IdentityServer.Web; ... typeof(AbpAccountPublicWebIdentityServerModule), typeof(AbpIdentityServerWebModule),替换为
using Volo.Abp.OpenIddict.Pro.Web; ... typeof(AbpAccountPublicWebOpenIddictModule), typeof(AbpOpenIddictProWebModule),在 MyApplicationWebModule.cs 的
ConfigureServices方法中更新身份验证配置:将
ConfigureAuthentication(context, configuration);替换为
ConfigureAuthentication(context);并将私有的
ConfigureAuthentication方法更新为:private void ConfigureAuthentication(ServiceConfigurationContext context) { context.Services.ForwardIdentityAuthenticationForBearer(OpenIddictValidationAspNetCoreDefaults.AuthenticationScheme); }注意:v6.0.0-rc.1 版本似乎使用
AddJwtBearer进行授权。此问题在后续版本中已修复。如果您使用的是 v6.0.0-rc.1,可以安全地删除 JWT 身份验证,并如上所示配置身份验证。在 MyApplicationWebModule.cs 中添加如下所示的
PreConfigureServices,并将您的应用程序名称作为受众:public override void PreConfigureServices(ServiceConfigurationContext context) { PreConfigure<OpenIddictBuilder>(builder => { builder.AddValidation(options => { options.AddAudiences("MyApplication"); // 替换为您的应用程序名称 options.UseLocalServer(); options.UseAspNetCore(); }); }); }在 MyApplicationWebModule.cs 的
OnApplicationInitialization方法中移除 IdentityServer 和 JwtToken 中间件:移除以下行:
app.UseIdentityServer();
Web 项目 (分层解决方案)
在 MyApplication.Web.csproj 中替换 项目引用:
<PackageReference Include="Volo.Abp.IdentityServer.Web" Version="6.0.*" />替换为
<PackageReference Include="Volo.Abp.OpenIddict.Pro.Web" Version="6.0.*" />在 MyApplicationWebModule.cs 中替换 using 语句和 模块依赖项:
using Volo.Abp.IdentityServer.Web; ... typeof(AbpIdentityServerWebModule),替换为
using Volo.Abp.OpenIddict.Pro.Web; ... typeof(AbpOpenIddictProWebModule),在 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 并添加 UsePkce 选项。
在 Menus 文件夹下的 MyApplicationMenuContributor.cs 文件中,于
ConfigureMainMenuAsync方法下替换 using 语句和菜单名称:using Volo.Abp.IdentityServer.Web.Navigation; ... //Administration->Identity Server administration.SetSubItemOrder(AbpIdentityServerMenuNames.GroupName, 2);替换为
using Volo.Abp.OpenIddict.Pro.Web.Menus; ... //Administration->OpenIddict administration.SetSubItemOrder(OpenIddictProMenus.GroupName, 2);
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 Microsoft.AspNetCore.Authentication.JwtBearer; using IdentityServer4.Configuration; ... 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.cs 的
ConfigureServices方法中,替换 ForwardIdentityAuthenticationForBearer:context.Services.ForwardIdentityAuthenticationForBearer();替换为
context.Services.ForwardIdentityAuthenticationForBearer(OpenIddictValidationAspNetCoreDefaults.AuthenticationScheme);在 MyApplicationIdentityServerModule.cs 的
ConfigureServices方法中,移除 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.cs 的
OnApplicationInitialization方法中替换 IdentityServer 和 JwtToken 中间件:app.UseJwtTokenMiddleware(); app.UseIdentityServer();替换为
app.UseAbpOpenIddictValidation();要使用新的 AuthServer 页面,请将 Index.cshtml.cs 替换为 AuthServer Index.cshtml.cs 以及将 Index.cshtml 文件替换为 AuthServer Index.cshtml。
注意:可以在 Pages 文件夹下找到。
Http.Api.Host
在 MyApplicationHttpApiHostModule.cs 的
OnApplicationInitialization方法中,删除app.UseAbpSwaggerUI选项配置中的c.OAuthClientSecret(configuration["AuthServer:SwaggerClientSecret"]);,该配置不再需要。在
appsettings.json中,从 AuthServer 部分删除 SwaggerClientSecret,如下所示:"AuthServer": { "Authority": "https://localhost:44345", "RequireHttpsMetadata": "false", "SwaggerClientId": "MyApplication_Swagger" },
抠丁客


