【问题标题】:Unable to use *.pfx certificate with kestrel inside windows service无法在 Windows 服务中使用 *.pfx 证书和红隼
【发布时间】:2020-08-01 01:57:29
【问题描述】:

我正在尝试使用 Windows 服务和 kestrel 配置我的 ASP.Net Core 3.1 应用程序。 到目前为止,该应用程序可以正常工作并启动。在控制台模式下也能正常工作。

但是,当我用我的证书(*.pfx 容器)添加 https 时,事情变得很奇怪。 该应用程序在控制台中运行没有问题,但在服务模式下失败并出现异常:

System.InvalidOperationException: Unable to configure HTTPS endpoint. No server certificate was specified, and the default developer certificate could not be found or is out of date.
To generate a developer certificate run 'dotnet dev-certs https'. To trust the certificate (Windows and macOS only) run 'dotnet dev-certs https --trust'.
For more information on configuring HTTPS see https://go.microsoft.com/fwlink/?linkid=848054.
   at Microsoft.AspNetCore.Hosting.ListenOptionsHttpsExtensions.UseHttps(ListenOptions listenOptions, Action`1 configureOptions)
   at Microsoft.AspNetCore.Hosting.ListenOptionsHttpsExtensions.UseHttps(ListenOptions listenOptions)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.AddressesStrategy.BindAsync(AddressBindContext context)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindAsync(IServerAddressesFeature addresses, KestrelServerOptions serverOptions, ILogger logger, Func`2 createBinding)
   at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer.StartAsync[TContext](IHttpApplication`1 application, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)
   at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)

使用 Microsoft.Extensions.Hosting.WindowsServices 配置服务模式。一切都是使用 ConfigureWebHostDefaults() 配置的,没有任何关于证书或任何东西的细节。这是来自 appsettings.json 的 sn-p:

"Urls": "https://*",
"Kestrel": {
    "Certificates": {
        "Default": {
            "Path": "full_path_to_my_certificate_container.pfx",
            "Password": "password"
        }
    }
}

在控制台模式下完美运行,证书应用正确,应用程序可访问。使用服务模式,无需任何配置更改。连环境都一样(其实连环境都没有设置,所以是一种“生产”)。 尝试将服务帐户切换到我的管理员帐户。完全没有区别。

我错过了什么?

UPDATE_01:

我已将我的配置从“*.pfx”证书更改为存储:

{
    "Kestrel": {
        "Certificates": {
            "Default": {
                "Location": "LocalMachine",
                "Store": "Root",
                "Subject": "MyCertSubjectName"
            }
        }
    }
}

和以前一样,这在控制台模式下可以完美运行,但在服务模式下却不会出现同样的异常。

UPDATE_02:

从此链接尝试了完整的“端点”块: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?view=aspnetcore-3.1#listenoptionsusehttps

完全没有区别。我可以看到我的配置使用正确,因为使用不正确的值对其进行了测试,并且应用程序在日志中报告了它们。

UPDATE_03:

在商店中玩弄我的证书的权限。没有运气。

我迷路了……

【问题讨论】:

    标签: asp.net-core https windows-services ssl-certificate


    【解决方案1】:

    我很惭愧... 似乎在服务模式下我的配置没有被读取。我只在控制台模式下测试这个事实(它更容易)并认为它在服务模式下完美运行,所以我排除了这部分。

    将其移至 appsettings.json 后一切正常。

    抱歉,如果有人现在正在输入该主题的 cmets...

    【讨论】:

      猜你喜欢
      • 2018-07-21
      • 1970-01-01
      • 2020-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-16
      • 1970-01-01
      • 2015-05-08
      相关资源
      最近更新 更多