简体   繁体   English

How to setup https when developing localy with webpack and hosting on Azure in Docker container running ASP.NET Core

[英]How to setup https when developing localy with webpack and hosting on Azure in Docker container running ASP.NET Core

I am hosting on Azure and have it configured to only allow https.我在 Azure 上托管,并将其配置为仅允许 https。 The backend is running ASP .NET Core in a Linux container.后端在 Linux 容器中运行 ASP .NET Core。 The webserver (Kestrel) is running without https enabled.网络服务器 (Kestrel) 在未启用 https 的情况下运行。 I've configured Azure TLS/SSL settings to force https, so when users connect from the public internet, they have to use https.我已经配置了 Azure TLS/SSL 设置来强制 https,所以当用户从公共互联网连接时,他们必须使用 https。 I have a cert that is signed by a cert authority and it's configured in the Azure App Service -> TLS/SSL -> Bindings settings.我有一个由证书颁发机构签名的证书,它是在 Azure 应用服务 -> TLS/SSL -> 绑定设置中配置的。

However in my local development environment I've been running webpack using http.但是在我的本地开发环境中,我一直在使用 http 运行 webpack。 So when I test I connect to localhost:8080 and this is redirected to localhost:8085 by webpack.因此,当我测试时,我连接到 localhost:8080,并被 webpack 重定向到 localhost:8085。 localhost:8085 is the port Kestrel is listening on. localhost:8085 是 Kestrel 正在侦听的端口。 I've decided I want to develop locally using https so that my environment mimics the production environment closely.我已经决定要使用 https 在本地进行开发,以便我的环境密切模仿生产环境。 To this I've started the webpack-dev-server with the --https command line option, and ammended my redirects in my webpack.config.js为此,我使用--https命令行选项启动了 webpack-dev-server,并在 webpack.config.js 中修改了我的重定向

For example:例如:

'/api/*': {
            target: 'https://localhost:' + (process.env.SERVER_PROXY_PORT || "8085"),
               changeOrigin: true,
               secure: false
           },

This redirects https requests to port 8085.这会将 https 请求重定向到端口 8085。

I've created a self-signed cert for use by Kestrel when developing locally.我创建了一个自签名证书,供 Kestrel 在本地开发时使用。 I modified my code to use this certificate as shown below:我修改了我的代码以使用此证书,如下所示:

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

This all works, and I can connect to webpack locally and it redirects the request to the webserver using https.这一切正常,我可以在本地连接到 webpack 并使用 https 将请求重定向到网络服务器。 The browser complains that the cert is insecure because it's self-signed but that was expected.浏览器抱怨证书不安全,因为它是自签名的,但这是意料之中的。

My question is how should this be setup in the production environment.我的问题是如何在生产环境中进行设置。 I don't want to be running the container on azure with the certificates I created locally embeded in the image.我不想在 azure 上使用我在本地创建的嵌入图像的证书运行容器。 In my production environment should I be configuring Kestrel, as I have done with the localhost code, to use the cert in loaded into Azure (as mentioned in the 1st paragraph)?在我的生产环境中,我是否应该像使用 localhost 代码一样配置 Kestrel,以使用加载到 Azure 中的证书(如第 1 段所述)? Or is simply binding it to the domain using the portal and forcing https via the Web UI enough?或者只是使用门户将其绑定到域并通过 Web UI 强制 https 就够了?

On Azure, If you have the PFX certificate you can choose to upload the certificate: see this image在 Azure 上,如果您有 PFX 证书,您可以选择上传证书:见此图

However, this certificate needs to come from a trusted certificate authority.但是,此证书需要来自受信任的证书颁发机构。

If the URL is a subdomain, you can choose a Free App Service Managed Certificate.如果 URL 是子域,您可以选择免费应用服务托管证书。

After, that all you need to do is enable https only in the portal.之后,您需要做的就是仅在门户中启用 https。

If its a naked domain and you really need the certificate to be free, you can get a certificate from sslforfree.com.如果它是一个裸域并且您确实需要免费的证书,您可以从 sslforfree.com 获得证书。 sslforfree will give you the.cer file and the private key you will need to generate a pfx. sslforfree 将为您提供 .cer 文件和生成 pfx 所需的私钥。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM