【问题标题】:How to setup https when developing localy with webpack and hosting on Azure in Docker container running ASP.NET Core如何在运行 ASP.NET Core 的 Docker 容器中使用 webpack 进行本地开发并在 Azure 上托管时设置 https
【发布时间】: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#


    【解决方案1】:

    在 Azure 上,如果您有 PFX 证书,则可以选择上传证书: see this image

    但是,此证书需要来自受信任的证书颁发机构。

    如果 URL 是子域,您可以选择免费应用服务托管证书。

    之后,您只需在门户中启用 https。

    如果它是一个裸域并且您确实需要免费的证书,您可以从 sslforfree.com 获取证书。 sslforfree 将为您提供 .cer 文件和生成 pfx 所需的私钥。

    【讨论】:

    • 我已经澄清了我的问题。我已经在 Azure 中安装了一个由 CA 签名的应用服务证书。
    • 我相信只要在weg GUI中绑定强制https就足够了
    • @sashan 如果这能澄清你的问题,请告诉我
    猜你喜欢
    • 2020-01-27
    • 2022-08-15
    • 2019-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-14
    相关资源
    最近更新 更多