ASP.NET Core 3.1 Web API:它可以作为带有 h​​ttps 的 Windows 服务自托管并使用一些像 IIS 的证书吗?

use*_*018 4 c# asp.net-core asp.net-core-webapi

我正在使用默认的 ASP.NET Core 3.1 Web API 应用程序,我已经对其进行了配置 https和使用app.UseHttpsRedirection();

现在我主持这个作为使用该NuGet包Windows服务:Microsoft.Extensions.Hosting.WindowsServices

托管是这样做,但我使用HTTP获取API的结果,但同时试图使用它造成的错误https一样https://localhost:5001/weatherforecast

我可以创建一些像 IIS 一样的自签名证书并分配它并可以作为 https 运行吗?

Gen*_*ato 5

@Frank Nielsen 答案是正确的,但如果其他人想要不同的方法,我想再添加一种方法。

创建证书的部分与@Franck Nielsen 在博客上发布的链接相同。就在这种方法中,所有配置都是通过appsettings.json.

ASP.NET Core 5.0文档说:

CreateDefaultBuilderConfigure(context.Configuration.GetSection("Kestrel"))默认调用以加载 Kestrel 配置。

所以你应该将“Kestrel”部分添加到 appsetting.json

{
  "Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "http://localhost:5000"
      },
      "Https": {
        "Url": "https://localhost:5001",
        "Certificate": {
          "Path": "<path to .pfx file>",
          "Password": "<certificate password>"
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Program.cs 无需额外配置 Kestrel 即可看起来像这样。

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

而中提琴,其余的都是由框架完成的......

我发现这种方法更简洁,并且没有像.exe在 Windows 服务中获取应用程序的根目录之类的麻烦。

链接到 ASP.NET Core 5.0 文档:为 ASP.NET Core Kestrel Web 服务器配置端点


Fra*_*sen 3

是的,您可以 - 但有一些浏览器限制。

创建一个证书,然后将其注册到证书存储中,或手动将其加载到 Kestrel 中,如下所示:

certificate.json

{
  "certificateSettings": {
    "fileName": "localhost.pfx",
    "password": "YourSecurePassword"
  }
}
Run Code Online (Sandbox Code Playgroud)

并像这样使用它:

public class Program
{
    public static void Main(string[] args)
    {
        var config = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddEnvironmentVariables()
            .AddJsonFile("certificate.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"certificate.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", optional: true, reloadOnChange: true)
            .Build();

        var certificateSettings = config.GetSection("certificateSettings");
        string certificateFileName = certificateSettings.GetValue<string>("filename");
        string certificatePassword = certificateSettings.GetValue<string>("password");

        var certificate = new X509Certificate2(certificateFileName, certificatePassword);

        var host = new WebHostBuilder()
            .UseKestrel(
                options =>
                {
                    options.AddServerHeader = false;
                    options.Listen(IPAddress.Loopback, 443, listenOptions =>
                    {
                        listenOptions.UseHttps(certificate);
                    });
                }
            )
            .UseConfiguration(config)
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseStartup<Startup>()
            .UseUrls("https://localhost:443")
            .Build();

        host.Run();
    }
}
Run Code Online (Sandbox Code Playgroud)

摘自这篇好博客文章的片段:https://www. humankode.com/asp-net-core/develop-locally-with-https-self-signed-certificates-and-asp-net-core