【问题标题】:.Net Core 2.2 API in Linux Service with SSL带有 SSL 的 Linux 服务中的 .Net Core 2.2 API
【发布时间】:2019-07-22 22:27:01
【问题描述】:

我有一个 .Net Core 2.2 API,它在从 linux 命令 shell(Ubuntu 16.04)启动时已经过测试并且可以工作,但是当作为服务运行时,只绑定了 http,而不是 https。我知道这更有可能是因为需要告知服务环境有关 SSL 的信息,但我还没有找到有关如何在服务单元文件中进行设置的任何信息。任何帮助将不胜感激。

服务单元文件:

[Unit]
Description=Authentication .NET Web API App running on CentOS 7

[Service]
WorkingDirectory=/var/www/Authentication
ExecStart=/usr/share/dotnet/dotnet /var/www/Authentication/Authentication.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-authentication
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production

[Install]
WantedBy=multi-user.target

控制台输出:

warn: Microsoft.AspNetCore.Server.Kestrel[0]
      Unable to bind to http://localhost:5000 on the IPv6 loopback interface: 'Cannot assign requested address'.
warn: Microsoft.AspNetCore.Server.Kestrel[0]
      Unable to bind to https://localhost:5001 on the IPv6 loopback interface: 'Cannot assign requested address'.
Hosting environment: Production
Content root path: /var/www/Authentication
Now listening on: http://localhost:5000
Now listening on: https://localhost:5001
Application started. Press Ctrl+C to shut down.

服务状态:

● Authentication.service - Authentication .NET Web API App running on CentOS 7
   Loaded: loaded (/etc/systemd/system/Authentication.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2019-07-22 06:48:36 CDT; 38min ago
 Main PID: 11506 (dotnet)
   CGroup: /system.slice/Authentication.service
           └─11506 /usr/share/dotnet/dotnet /var/www/Authentication/Authentication.dll

Jul 22 06:48:38 Chris-PC dotnet-authentication[11506]: warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[59]
Jul 22 06:48:38 Chris-PC dotnet-authentication[11506]:       Neither user profile nor HKLM registry available. Using an ephemeral key repository. Protected data will be unavailable when application exits.
Jul 22 06:48:38 Chris-PC dotnet-authentication[11506]: warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
Jul 22 06:48:38 Chris-PC dotnet-authentication[11506]:       No XML encryptor configured. Key {bc217aa5-1a54-4033-bcb3-0d1bd39cf425} may be persisted to storage in unencrypted form.
Jul 22 06:48:40 Chris-PC dotnet-authentication[11506]: warn: Microsoft.AspNetCore.Server.Kestrel[0]
Jul 22 06:48:40 Chris-PC dotnet-authentication[11506]:       Unable to bind to http://localhost:5000 on the IPv6 loopback interface: 'Cannot assign requested address'.
Jul 22 06:48:40 Chris-PC dotnet-authentication[11506]: Hosting environment: Production
Jul 22 06:48:40 Chris-PC dotnet-authentication[11506]: Content root path: /var/www/Authentication
Jul 22 06:48:40 Chris-PC dotnet-authentication[11506]: Now listening on: http://localhost:5000
Jul 22 06:48:40 Chris-PC dotnet-authentication[11506]: Application started. Press Ctrl+C to shut down.

journalctl 输出:

Jul 22 06:48:36 Chris-PC systemd[1]: Started Authentication .NET Web API App running on CentOS 7.
Jul 22 06:48:38 Chris-PC dotnet-authentication[11506]: : Microsoft.AspNetCore.DataProtection.Repositories.EphemeralXmlRepository[50]
Jul 22 06:48:38 Chris-PC dotnet-authentication[11506]:       Using an in-memory repository. Keys will not be persisted to storage.
Jul 22 06:48:38 Chris-PC dotnet-authentication[11506]: warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[59]
Jul 22 06:48:38 Chris-PC dotnet-authentication[11506]:       Neither user profile nor HKLM registry available. Using an ephemeral key repository. Protected data will be unavailable when application exits.
Jul 22 06:48:38 Chris-PC dotnet-authentication[11506]: warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
Jul 22 06:48:38 Chris-PC dotnet-authentication[11506]:       No XML encryptor configured. Key {bc217aa5-1a54-4033-bcb3-0d1bd39cf425} may be persisted to storage in unencrypted form.
Jul 22 06:48:40 Chris-PC dotnet-authentication[11506]: warn: Microsoft.AspNetCore.Server.Kestrel[0]
Jul 22 06:48:40 Chris-PC dotnet-authentication[11506]:       Unable to bind to http://localhost:5000 on the IPv6 loopback interface: 'Cannot assign requested address'.
Jul 22 06:48:40 Chris-PC dotnet-authentication[11506]: Hosting environment: Production
Jul 22 06:48:40 Chris-PC dotnet-authentication[11506]: Content root path: /var/www/Authentication
Jul 22 06:48:40 Chris-PC dotnet-authentication[11506]: Now listening on: http://localhost:5000
Jul 22 06:48:40 Chris-PC dotnet-authentication[11506]: Application started. Press Ctrl+C to shut down.

【问题讨论】:

  • journalctl -u Authentication 输出也会很有用。
  • 添加了 journalctl 输出,但显示与 systemctl 状态输出相同。我不是 Linux 专家,所以不确定如何在服务环境中设置 ssl。
  • journalctl 行似乎在右侧被截断。有什么办法可以得到完整的台词?
  • 抱歉,已编辑 journalctl 输出。同样,不是 Linux 专家。

标签: linux ssl .net-core


【解决方案1】:

发现问题与 www-data 用户 ID 有关。如果我编辑服务单元以使用我的 id(id 在命令 shell 下运行),则 http 和 https 都被绑定。需要弄清楚 www-data 的不同之处。

systemctl 状态:

● Authentication.service - Authentication .NET Web API App running on CentOS 7
   Loaded: loaded (/etc/systemd/system/Authentication.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2019-07-22 08:17:05 CDT; 8min ago
 Main PID: 19440 (dotnet)
   CGroup: /system.slice/Authentication.service
           └─19440 /usr/share/dotnet/dotnet /var/www/Authentication/Authentication.dll

Jul 22 08:17:05 Chris-PC systemd[1]: Started Authentication .NET Web API App running on CentOS 7.
Jul 22 08:17:10 Chris-PC dotnet-authentication[19440]: warn: Microsoft.AspNetCore.Server.Kestrel[0]
Jul 22 08:17:10 Chris-PC dotnet-authentication[19440]:       Unable to bind to http://localhost:5000 on the IPv6 loopback interface: 'Cannot assign requested address'.
Jul 22 08:17:11 Chris-PC dotnet-authentication[19440]: warn: Microsoft.AspNetCore.Server.Kestrel[0]
Jul 22 08:17:11 Chris-PC dotnet-authentication[19440]:       Unable to bind to https://localhost:5001 on the IPv6 loopback interface: 'Cannot assign requested address'.
Jul 22 08:17:11 Chris-PC dotnet-authentication[19440]: Hosting environment: Production
Jul 22 08:17:11 Chris-PC dotnet-authentication[19440]: Content root path: /var/www/Authentication
Jul 22 08:17:11 Chris-PC dotnet-authentication[19440]: Now listening on: http://localhost:5000
Jul 22 08:17:11 Chris-PC dotnet-authentication[19440]: Now listening on: https://localhost:5001
Jul 22 08:17:11 Chris-PC dotnet-authentication[19440]: Application started. Press Ctrl+C to shut down.

【讨论】:

    猜你喜欢
    • 2019-12-11
    • 1970-01-01
    • 2019-05-25
    • 1970-01-01
    • 1970-01-01
    • 2019-11-22
    • 2020-02-20
    • 2021-03-28
    • 2020-10-21
    相关资源
    最近更新 更多