项目

IdentityServer 部署

IdentityServer 的配置可能因部署环境而异。基本上,你需要根据你的部署环境更新 IdentityServer 客户端相关数据并调整你的托管偏好设置。

更新 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

如果使用 AngularBlazor(Web Assembly)作为后台 Web 应用程序,则必须进行此配置。该配置位于 appsettings.json 中的 App 配置下。

"RedirectAllowedUrls": "http://localhost:4200,https://localhost:44307"

更新数据库迁移工具 (DbMigrator)

IdentityServerDataSeedContributor 使用 appsettings.jsonIdentityServer.Clients 部分来获取 ClientIdRedirectUriPostLogoutRedirectUriCorsOrigins

更新 DbMigrator 项目的 appsettings.json 中的 IdentityServer.Clients.RootUrls,填入生产环境的值:

db-migrator-appsettings

或者,手动向数据库中的 IdentityServerClientRedirectUrisIdentityServerClientPostLogoutRedirectUrisIdentityServerClientCorsOrigins 表添加生产环境的值。

如果你使用的是微服务模板的实时迁移,并且没有使用 dbmigrator 项目,请更新 IdentityService 的 appsettings。

最终,配置中不应再包含 localhost 相关的数据。

更新 IdentityServer

你需要根据你的托管环境更新令牌签名证书和 IdentityServer 中间件。

签名证书

默认的开发环境使用开发者签名证书选项。在生产环境中使用开发者签名证书可能会导致 IDX10501: 签名验证失败 错误。

更新 IdentityServerModule,在 IIdentityServerBuilder 的预配置中使用真实的证书。

idsrv-certificate

你也可以创建自签名证书并使用它。

如果你使用的是自签名证书,请不要忘记将证书(.pfx 文件)设置为 EmbeddedResource 并设置 CopyToOutputDirectory。该文件需要物理存在。

使用 HTTPS

更新 IdentityServerModule强制使用 HTTPS。添加 UseHsts 来向客户端添加 HSTS 标头,添加 UseHttpsRedirection 来将 HTTP 请求重定向到 HTTPS。

use-https

位于负载均衡器之后

要将来自负载均衡器的 HTTP 请求重定向到 HTTPS,请使用下面的中间件更新 IdentityServerModuleOnApplicationInitialization 方法:

app.Use((httpContext, next) =>
{
    httpContext.Request.Scheme = "https";
    return next();
});

Kubernetes

一个常见的场景是在 Kubernetes 环境中运行应用程序。虽然 IdentityServer 需要通过 HTTPS 面向互联网,但内部请求可以通过 HTTP 完成。

idsrv-k8s

HttpApi.HostWeb 应用程序的颁发机构 (Authority) 应设置为 HTTP,因为令牌验证将通过 HTTP 请求完成。

api-resource-internal-idsrv

你可以使用不同的 appsettings 文件(例如 appsettings.production.json)来覆盖这些值,或者直接从 Kubernetes 覆盖环境变量。

要将内部 IdentityServer 请求与外部网络(互联网)隔离,可以附加额外的标头而不是覆盖。 对于 Ingress,你可以使用 nginx.ingress.kubernetes.io/configuration-snippet

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: myidentityserver-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
    nginx.ingress.kubernetes.io/proxy-buffer-size: "32k"
    nginx.ingress.kubernetes.io/proxy-buffers-number: "8"
    nginx.ingress.kubernetes.io/configuration-snippet: |
      more_set_input_headers "from-ingress: true";
spec:

你需要根据标头来设置 IdentityServer 的源。使用下面的中间件更新 IdentityServerModuleOnApplicationInitialization 方法:

app.Use(async (ctx, next) =>
{
    if (ctx.Request.Headers.ContainsKey("from-ingress"))
    {
        ctx.SetIdentityServerOrigin("https://myidentityserver.com");
    }

    await next();
});
在本文档中