简体   繁体   English

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 : can it be self-hosted as Windows service with https and use some certificate like IIS?

I am using default ASP.NET Core 3.1 Web API app where I have configured it for https and using app.UseHttpsRedirection(); I am using default ASP.NET Core 3.1 Web API app where I have configured it for https and using app.UseHttpsRedirection(); as well.以及。

Now I am hosting this as a Windows service using this nuget package: Microsoft.Extensions.Hosting.WindowsServices .现在,我使用此 nuget package: Microsoft.Extensions.Hosting.WindowsServices将其托管为 Windows 服务。

Hosting is done but I am getting the API result using http, but it's causing an error while trying to use https like https://localhost:5001/weatherforecast Hosting is done but I am getting the API result using http, but it's causing an error while trying to use https like https://localhost:5001/weatherforecast

Can I create some self signed certificate like IIS and assign it and can run as https?我可以创建一些像 IIS 这样的自签名证书并将其分配并可以作为 https 运行吗?

@Frank Nielsen answer is correct, but i wanted to add one more method if someone else wanted different approach. @Frank Nielsen 的回答是正确的,但如果其他人想要不同的方法,我想再添加一种方法。

Part with creating certificate is the same as on the blog that @Franck Nielsen posted link to it.创建证书的部分与@Franck Nielsen 在博客上发布的链接相同。 Just in this approach all configuration is done automatically through appsettings.json .就在这种方法中,所有配置都是通过appsettings.json自动完成的。

ASP.NET Core 5.0 docs says: ASP.NET 核心 5.0文档说:

CreateDefaultBuilder calls Configure(context.Configuration.GetSection("Kestrel")) by default to load Kestrel configuration. CreateDefaultBuilder默认调用Configure(context.Configuration.GetSection("Kestrel"))来加载 Kestrel 配置。

So you should add "Kestrel" section to appsetting.json所以你应该将“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 could look like this with no additional configuring of Kestrel. Program.cs可能看起来像这样,无需额外配置 Kestrel。

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

And viola, rest is done by framework...而中提琴,rest 是由框架完成的......

I found this method more cleaner and there is no hustle with things like getting the root directory of application .exe in windows service.我发现这种方法更干净,并且没有像在 windows 服务中获取应用程序.exe的根目录这样的麻烦。

Link to ASP.NET Core 5.0 docs: Configure endpoints for the ASP.NET Core Kestrel web server链接到 ASP.NET Core 5.0 文档: 为 ASP.NET Core Kestrel web 服务器配置端点

Yes, you can - but with some browser restrictions.是的,你可以 - 但有一些浏览器限制。

Create a certificate, and then either register it in certificate store, or load it in manually into Kestrel like:创建一个证书,然后在证书存储中注册它,或者手动将其加载到 Kestrel 中,例如:

certificate.json

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

and use it something like this:并像这样使用它:

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

Snippets taken from this good blog post: https://www.humankode.com/asp-net-core/develop-locally-with-https-self-signed-certificates-and-asp-net-core摘自这篇优秀的博文: https://www.humankode.com/asp-net-core/develop-locally-with-https-self-signed-certificates-and-asp-net-core

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在ASP.NET自托管Web API上配置SSL - Configuring SSL on ASP.NET Self-Hosted Web API 自托管的OWIN .NET 4.5 Web API 2不适用于Windows身份验证和https - Self-hosted OWIN .NET 4.5 Web API 2 does not work with Windows authentication and https 自托管进程中 Web API 带点网核心 - Self-hosted In Process Web API with Dot net core 如何在asp.net自托管API中启用CORS? - How to enable CORS in asp.net Self-Hosted API? https在Z1848E7322214CCE460F9B24F9B24F5E72Z CORE 3.1 Z2567A.AS SERSER 3.1 - force https on an asp.net core 3.1 web app running as a service on a windows server 2008 rs server 更新配置并根据HTTP请求重新启动自托管的ASP.NET Web API 2 - Update configuration and restart self-hosted ASP.NET Web API 2 on HTTP request .NET 6 中的自托管 Web API - Self-Hosted Web API in .NET 6 ASP.NET 内核 Web 3.1 - IIS Express 可以从 localdb 获取数据,但本地 Z5DA5ACF461B4EFB27E6ZEC6 不能 - ASP.NET Core Web 3.1 - IIS Express can fetch data from localdb but local IIS can not 从asp.net网站访问本地自托管WCF服务 - access local self-hosted WCF service from asp.net Website 转换为ASP.Net Framework 4.5.1如何影响自托管的Web服务? - How does conversion to ASP.Net Framework 4.5.1 affect self-hosted web services?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM