.NET Core Linux Kestrel https 配置

san*_*hec 5 linux ssl https asp.net-core

我们正在 Linux(Ubuntu 18.04 LTS 和 Apache2)上部署我们的第一个 .NET Core 应用程序。

我们不知道部署它们的服务器的证书,也不知道它们将部署的端口,因为它们是客户端的,我们没有访问权限,所以我们需要能够通过 appsettings 中的配置输入它们(红隼配置)。

在 Windows 中,api 在 http 和 https 中都可以正常工作,将此配置放入appsettings.json并在Startup.cs 中读取它,如下所示:

// kestrel configuration
services.Configure<KestrelServerOptions>(Configuration.GetSection("Kestrel"));
Run Code Online (Sandbox Code Playgroud)

我们的windows配置appsettings.json是:

"AllowedHosts": "*.mydomain.es;*.mydomain-eu.com;test-win;test-linux;localhost;127.0.0.1;*.myActiveDirectoryDomain.ad",
"Kestrel": {
"Endpoints": {
  "Http": {
    "Url": "http://localhost:5009"
  }
  ,"Https": {
    "Url": "https://localhost:5010"    
  }
}
}
Run Code Online (Sandbox Code Playgroud)

使用相同配置部署在 Linux 上时,Kestrel 服务不会启动。红隼服务错误:

须藤 systemctl 状态 kestrel-apieu.service ?kestrel-apieu.service - 在 Ubuntu 18.04 上运行的示例 ASP .NET Api 加载:加载(/etc/systemd/system/kestrel-apieu.service;启用;供应商预设:启用)活动:激活(自动重启)(结果: core-dump ) 自 2020-02-06 星期四 09:13:20 CET; 4s 前进程:4449 ExecStart=/usr/bin/dotnet /var/www/core/api/apieu/HHHHH.JJJJJJJJ.Api.UnitsEuApi.dll (code=dumped, signal=ABRT) Main PID: 4449 ( code=dumped) , 信号=ABRT )

删除 https 部分可以在 http 中正常工作,没有任何问题,如下所示:

"Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "http://localhost:5009"
      } 
    }
  }
Run Code Online (Sandbox Code Playgroud)

正在运行的 Kestrel 服务:

须藤 systemctl 状态 kestrel-apieu.service ?kestrel-apieu.service - 在 Ubuntu 18.04 上运行的示例 ASP .NET Api 已加载:已加载(/etc/systemd/system/kestrel-apieu.service;已启用;供应商预设:已启用)活动:自 2020 年 2 月 2 日星期四起处于活动状态(正在运行) -06 09:16:19 欧洲中部时间;2s 前 Main PID: 5504 (dotnet) Tasks: 17 (limit: 4660) CGroup: /system.slice/kestrel-apieu.service ??5504 /usr/bin/dotnet /var/www/core/api/apieu/HHHHH .JJJJJJJJ.Api.UnitsEuApi.dll

当我们将此配置设置为服务器的自签名证书 .crt 时,Kestrel 服务会提升但不适用于 https。

配置appsetings

"AllowedHosts": "*.mydomain.es;*.mydomain-eu.com;test-win;test-linux;localhost;127.0.0.1;*.myActiveDirectoryDomain.ad",
"Kestrel": {
"Endpoints": {
  "Http": {
    "Url": "http://localhost:5009"
  }
  ,"Https": {
    "Url": "https://localhost:5010", // we also tried: "https://*:5010"
    "Certificate": {
      "Path": "/etc/apache2/ssl/apache.crt",
      "Password": "/etc/apache2/ssl/apache.key",
      "AllowInvalid": true
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

http://localhost:5009/test 工作正常,但 https://localhost:5010/test 发送错误:

安全连接失败

连接到 localhost:5010 期间发生错误。PR_END_OF_FILE_ERROR

The page you are trying to view cannot be shown because the authenticity of the received data could not be verified.
Please contact the website owners to inform them of this problem.
Run Code Online (Sandbox Code Playgroud)

但是自签名证书确实允许您毫无问题地输入 https://localhost(一旦您信任该证书)。

我们还尝试将自签名 .crt 证书转换为 .pfx(使用 OpenSSL ->将 crt 转换为 pfx 证书)并以这种方式配置:

"Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "http://localhost:5009"
      }
    ,"Https": {
        "Url": "https://*:5010",
        "Certificate": {
          "Path": "/etc/apache2/ssl/apache.pfx",
          "Password": "passwdExport"
          ,"AllowInvalid": true
        }
        }

    }
  }
Run Code Online (Sandbox Code Playgroud)

但它也不会解除服务,并且它不适用于 http 或 https。

我们查看了所有这些帮助页面,其中包括:

.net 核心 Kestrel 服务器 SSL 问题

使用 .net Core 3.1 的 Kestrel ssl JSON 配置中的证书问题

Asp.Net Core 2.0 HTTP -> Kestrel 上的 HTTPS

自签名证书是否可以在 Apache 反向代理后面工作?

问题似乎是我们没有使用自签名证书正确配置 Kestrel。但是我们找不到我们的错误。你能帮我们吗?

如果你能提供更多信息,我们打开了之前的另一篇文章,它更通用,因为我们不知道问题来自 Kestrel,但我们认为它来自 Apache2:部署 NET Core Linux HTTPS SSL

小智 1

我使用 .net6 并建议您尝试以下操作:

"Kestrel": {
    "EndPoints": {
      "Https": {
        "Url": "https://domain:port",
        "Certificate": {
          "Path": "path_to.pfx",
          "Password": "password"
        }

      }
    }
  }
Run Code Online (Sandbox Code Playgroud)

在您的 Program.cs 文件中输入以下内容:

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                #if RELEASE                 
                    webBuilder.UseKestrel();
                #endif
                    webBuilder.UseStartup<Startup>();
                }); 
Run Code Online (Sandbox Code Playgroud)

它会对你有所帮助!还有有用的链接:

https://learn.microsoft.com/en-us/answers/questions/613333/loading-certificatepfx-with-password-in-linux-does.html

https://learn.microsoft.com/ru-ru/aspnet/core/fundamentals/servers/kestrel/endpoints?view=aspnetcore-6.0