【问题标题】:ASP.NET Core 3.1 Web API : can it be self-hosted as Windows service with https and use some certificate like IIS?ASP.NET Core 3.1 Web API:它可以通过https自托管为Windows服务并使用像IIS这样的证书吗?
【发布时间】:2020-07-23 02:41:19
【问题描述】:

我正在使用默认的 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 运行吗?

【问题讨论】:

  • https 请求的错误是什么 - 堆栈跟踪?
  • localhost 拒绝连接。尝试:检查连接检查代理和防火墙
  • 但是您在收听443 (ssl) 吗?尝试运行netstat -aon -p tcp|findstr LISTENING 并查看443 是否存在。

标签: c# asp.net-core asp.net-core-webapi


【解决方案1】:

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

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

certificate.json

{
  "certificateSettings": {
    "fileName": "localhost.pfx",
    "password": "YourSecurePassword"
  }
}

并像这样使用它:

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();
    }
}

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

【讨论】:

  • 好的,谢谢。会调查一下。
【解决方案2】:

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

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

ASP.NET Core 5.0 文档说:

CreateDefaultBuilder 默认调用Configure(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>"
        }
      }
    }
  }
}

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

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

还有中提琴,其余的都是由框架完成的……

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

链接到 ASP.NET Core 5.0 文档:Configure endpoints for the ASP.NET Core Kestrel web server

【讨论】:

    猜你喜欢
    • 2023-01-31
    • 2012-02-23
    • 1970-01-01
    • 2020-09-15
    • 1970-01-01
    • 1970-01-01
    • 2019-11-30
    • 2019-08-29
    • 1970-01-01
    相关资源
    最近更新 更多