OpenIddict 部署
OpenIddict 是通过 OpenIddict 模块 在 ABP 模板中使用的默认 OpenId 提供程序库。在分层/独立 AuthServer 的应用程序模板中,它由 AuthServer 项目托管。对于非分层应用程序,它由 Web (MVC/Razor)、BlazorServer 或针对 Blazor 和 Angular 应用程序的 HttpApi.Host 项目托管。
更新 CORS 源
部署时必须更新网关、微服务 Swagger 授权以及 Angular/Blazor(Web Assembly)的 CORS 源配置。此配置可在 appsettings.json 的 App 配置下找到。
"CorsOrigins": "https://*.MyProjectName.com,http://localhost:4200,https://localhost:44307,https://localhost:44325,https://localhost:44353,https://localhost:44367,https://localhost:44388,https://localhost:44381,https://localhost:44361",
更新重定向允许的 URL
如果使用 Angular 或 Blazor(Web Assembly)作为后台 Web 应用程序,则必须进行此配置。它位于 appsettings.json 的 App 配置下。
"RedirectAllowedUrls": "http://localhost:4200,https://localhost:44307"
更新 DbMigrator
OpenIddictDataSeedContributor 使用 appsettings.json 的 OpenIddict.Applications 部分来获取 ClientId、RedirectUri、PostLogoutRedirectUri 和 CorsOrigins。
使用生产值更新 DbMigrator 项目的 appsettings.json 中的 OpenIddict.Applications.RootUrls,或覆盖它们:
如果你使用微服务模板的自迁移功能且不使用 DbMigrator 项目,请更新 IdentityService 的 appsettings。
最终,你不应包含任何与 localhost 相关的数据。
更新 AuthServer
在开发环境中,OpenIddict 使用开发加密和签名证书。在生产环境中,必须禁用此功能。OpenIddict 需要真实的证书来对令牌进行签名和加密。
签名和加密证书
默认的开发环境使用开发人员签名证书选项。在生产环境中使用开发人员签名证书可能会导致 IDX10501: Signature validation failed 错误。
在 OpenIddictBuilder 预配置中,使用真实证书更新 AuthServerModule。
当你从应用程序模板创建新应用程序时,ABP CLI 会自动生成一个名为 openiddict.pfx 的新自签名证书和一个随机密码。此文件和密码在 GetSigningCertificate 方法中提供。
注意:如果你收到关于无法在服务器上访问
openiddict.pfx文件的错误,请确保你拥有必要的权限。
存储证书的最佳位置取决于你的主机环境:
- 对于 IIS 应用程序,将证书存储在计算机存储中是推荐选项。
- 在 Azure 上,可以使用特殊的
WEBSITE_LOAD_CERTIFICATES标志将证书上传并暴露给 Azure App Service 应用程序。更多信息,请访问在 Azure App Service 的代码中使用 TLS/SSL 证书文档。
有关更多信息以及使用不同类型的签名/加密密钥,请查看 OpenIddict 文档。
使用或禁用 HTTPS
托管 OpenIddict openid-provider 库的 AuthServer 使用 ASP.NET Core 中间件的 SSL/TLS 绑定。如果你在 HTTPS 上托管它,Issuer 将在 HTTPS 上托管。
在某些部署场景中,你可能会遇到错误:
error: invalid_request
error_description: This server only accepts HTTPS requests.
error_uri: https//documnentation.openiddict.com/errors/ID2083
你可以轻松地从 appsettings.json 中禁用 HTTPS 要求:
"AuthServer": {
"Authority": "https://localhost:44369",
"RequireHttpsMetadata": "false"
},
此配置可以在 AuthServer 项目的 ConfigureServices 方法中找到:
if (!Convert.ToBoolean(configuration["AuthServer:RequireHttpsMetadata"]))
{
Configure<OpenIddictServerAspNetCoreOptions>(options =>
{
options.DisableTransportSecurityRequirement = true;
});
}
位于负载均衡器后
如果你使用 Nginx 或 Kubernetes Nginx Ingress,你可能需要转发请求头。
在 AuthServerModule 的 ConfigureServices 方法中配置选项:
Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
});
并在 AuthServerModule 的 OnApplicationInitialization 方法中使用中间件:
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseForwardedHeaders();
...
有时,可能无法在代理到应用程序的请求中包含转发的请求头。
如果代理强制要求所有公共外部请求都使用 HTTPS,可以在使用任何中间件之前手动设置方案。
在 AuthServerModule 的 OnApplicationInitialization 方法下进行配置:
app.Use((httpContext, next) =>
{
httpContext.Request.Scheme = "https";
return next();
});
常见问题
- 我看到
Server Error 502!- 检查 Logs 文件夹下的应用程序日志。配置错误可能会阻止你的应用程序启动,最简单的调试方法是查看日志以定位问题。
System.IO.FileNotFoundException: Signing Certificate couldn't found!:- 确保相关位置存在 .pfx 文件。.pfx 文件应标记为嵌入式资源,并且在发布应用程序时应位于发布目录中。
- 我看不到登录页面!它显示
HTTP 400错误。- 这与尝试针对 AuthServer 进行身份验证的应用程序生成的 URL 有关。检查 AuthServer 日志,并确保你拥有从 OpenIddictDataSeedContributor 播种的有效 redirect_uri,并且重定向到 AuthServer 的应用程序具有相同的配置。
抠丁客




