【发布时间】:2020-11-29 00:01:42
【问题描述】:
我在 Azure 上托管,并将其配置为仅允许 https。后端在 Linux 容器中运行 ASP .NET Core。网络服务器 (Kestrel) 在未启用 https 的情况下运行。我已将 Azure TLS/SSL 设置配置为强制使用 https,因此当用户从公共 Internet 连接时,他们必须使用 https。我有一个由证书颁发机构签名的证书,它是在 Azure 应用服务 -> TLS/SSL -> 绑定设置中配置的。
但是在我的本地开发环境中,我一直在使用 http 运行 webpack。因此,当我测试时,我连接到 localhost:8080,这被 webpack 重定向到 localhost:8085。 localhost:8085 是 Kestrel 正在侦听的端口。我已经决定要使用 https 在本地进行开发,以便我的环境密切模仿生产环境。为此,我使用 --https 命令行选项启动了 webpack-dev-server,并在 webpack.config.js 中修改了我的重定向
例如:
'/api/*': {
target: 'https://localhost:' + (process.env.SERVER_PROXY_PORT || "8085"),
changeOrigin: true,
secure: false
},
这会将 https 请求重定向到端口 8085。
我创建了一个自签名证书,供 Kestrel 在本地开发时使用。我修改了我的代码以使用此证书,如下所示:
let configure_host (settings_file : string) (builder : IWebHostBuilder) =
//turns out if you pass an anonymous function to a function that expects an Action<...> or
//Func<...> the type inference will work out the inner types....so you don't need to specify them.
builder.ConfigureAppConfiguration((fun ctx config_builder ->
config_builder
.SetBasePath(Directory.GetCurrentDirectory())
.AddEnvironmentVariables()
.AddJsonFile(settings_file, false, true)
.Build() |> ignore))
.ConfigureKestrel(fun ctx opt ->
eprintfn "JWTIssuer = %A" ctx.Configuration.["JWTIssuer"]
eprintfn "CertificateFilename = %A" ctx.Configuration.["CertificateFilename"]
let certificate_file = (ctx.Configuration.["CertificateFilename"])
let certificate_password = (ctx.Configuration.["CertificatePassword"])
let certificate = new X509Certificate2(certificate_file, certificate_password)
opt.AddServerHeader <- false
opt.Listen(IPAddress.Loopback, 8085, (fun opt -> opt.UseHttps(certificate) |> ignore)))
.UseUrls("https://localhost:8085") |> ignore
builder
这一切正常,我可以在本地连接到 webpack,它使用 https 将请求重定向到 web 服务器。浏览器抱怨证书不安全,因为它是自签名的,但这是意料之中的。
我的问题是如何在生产环境中进行设置。我不想使用我在本地创建的嵌入图像的证书在 azure 上运行容器。在我的生产环境中,我是否应该像使用 localhost 代码一样配置 Kestrel,以使用加载到 Azure 中的证书(如第 1 段所述)?还是只是使用门户将其绑定到域并通过 Web UI 强制 https 就足够了?
【问题讨论】:
标签: asp.net azure ssl webpack f#