gpt4 book ai didi

c# - ASP.NET Core 3.1 Web API : can it be self-hosted as Windows service with https and use some certificate like IIS?

转载 作者:行者123 更新时间:2023-12-03 20:25:55 24 4
gpt4 key购买 nike

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

最佳答案

@Frank Nielsen 答案是正确的,但如果其他人想要不同的方法,我想再添加一种方法。
创建证书的部分与@Franck Nielsen 在博客上发布的链接相同。就在这种方法中,所有配置都是通过 appsettings.json 自动完成的。 .
ASP.NET 核心 5.0 文档说:

CreateDefaultBuilder calls Configure(context.Configuration.GetSection("Kestrel")) by default to load Kestrel configuration.


所以你应该将“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();
而中提琴,其余的都是由框架完成的......
我发现这种方法更简洁,并且没有像获取应用程序的根目录之类的麻烦 .exe在 Windows 服务中。
链接到 ASP.NET Core 5.0 文档: Configure endpoints for the ASP.NET Core Kestrel web server

关于c# - ASP.NET Core 3.1 Web API : can it be self-hosted as Windows service with https and use some certificate like IIS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61138759/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com