[英]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
callsConfigure(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.