2

我在.Net Core 上使用 Kestrel 实现基本 SSL 服务器时遇到了一些问题。

我的目标是最终创建我自己的 Alexa 技能端点,我已经在树莓派上的烧瓶上运行了一个基本版本。但我是一个 .net 人,我真的很想利用我所知道的并且可以更好地理解 .net 核心在 Raspberry Pi 3 上的 Windows IOT 上的内容。

我遵循了一些关于如何使用 Kestrel 实现 SSL 服务器的非常模糊的教程,除了一个问题外,我可以启动并运行它。唯一有效的 SSL 证书是使用 powershell 创建的 windows 生成的 PFX 证书。

在我尝试将证书上传到技能端点中的 Alexa 开发人员控制台之前,这不是问题,它不接受 PFX。

因此,我切换到 openssl 并创建了一个证书,我的代码似乎可以接受并运行该证书,而且我还可以将 .cer 文件上传到 Alexa Skills 中。

问题是当任何尝试连接到服务器时,我得到以下异常:

Hosting environment: Production
Content root path: C:\Users\stewj\OneDrive\Documents\Visual Studio 2017 Projects\ConsoleApp1\ConsoleApp1\bin\Debug\netcoreapp2.0
Now listening on: http://0.0.0.0:8000
Now listening on: https://0.0.0.0:8001
Application started. Press Ctrl+C to shut down.

fail: Microsoft.AspNetCore.Server.Kestrel[0]
  Uncaught exception from the OnConnectionAsync method of an IConnectionAdapter.
System.ComponentModel.Win32Exception (0x80004005): The credentials supplied to the package were not recognized

或者

"The server mode SSL must use a certificate with the associated private key."

理想的情况是能够创建一个看起来可以正常工作的 PFX 证书(我可以浏览到服务器并且没有任何异常,只是警告说非受信任的 CA 作为其自签名)。并从 Alexa Skills 接受的 PFX 文件生成等效的 .CER 文件。

不幸的是,我是 SSL 和证书的新手,只有基本的了解,所以我不确定真正的潜在问题是什么。

这是服务器代码,以防我遗漏任何东西:

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>

        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .UseKestrel(options =>
            {
                var cert = new X509Certificate2("cert-key-20180708-190801.p12", "likeiwouldpastethathere");
                options.Listen(IPAddress.Any, 8000);
                options.Listen(IPAddress.Any, 8001, listenOptions =>
                {
                    listenOptions.UseHttps(cert);
                });
            });
}

顺便说一句,我也尝试过直接从 .UseHttps 加载证书,但存在同样的问题。使用 x509Certificate2 是一个想法,也许这是正确的方法,但仍然没有乐趣。

应用程序运行良好只有当我从 Alexa 或浏览器访问服务器时才会出现异常。如果我使用 PFX windows 生成的证书无法从 Alexa 访问,则没有例外!

4

0 回答 0