我是 .net core 的新手,并將 .Net core5 RestAPI 作為 WindowsService 托管。但問題是我能夠打開 http 鏈接但 https 總是給出錯誤ERR_CONNECTION_REFUSED
當我使用 IIS Express 在除錯模式下啟動它時,我可以訪問 http 和 https,但是當我發布并創建和啟動 windows 服務時,只有 http 鏈接有效。
正如您在下面看到的,我還將 URL 配置為使用帶有埠號的 https,沒有運氣!但是一個更奇怪的問題是在將其作為 Windows 服務托管后,我只能在 5000 埠上訪問這些 url,而不是我指定的埠。
我還在我的本地主機上安裝了證書。
我花了一整天的時間,也是.net核心的新手,不確定我做錯了什么,任何幫助將不勝感激。
程式類:
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseUrls("https://*:44392");
webBuilder.UseStartup<Startup>();
}).UseWindowsService();
啟動設定.Json
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:61198",
"sslPort": 44392
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "weatherforecast",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"ancmHostingModel": "InProcess"
}
}
}
啟動.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
uj5u.com熱心網友回復:
我可以通過指定我自己的 Kestrel 可以使用的證書來解決這個問題,因為我使用 Windows 服務來托管我的 API,所以它沒有檢測到現有的證書,我在這里使用了教程:
https://www.humankode.com/asp-net-core/develop-locally-with-https-self-signed-certificates-and-asp-net-core
但現在我遇到了另一個問題,jwt 身份驗證給出了問題......
如果我找到 jwt 的解決方案,我會回復。
System.InvalidOperationException:方案已存在:承載
更新:我指定了 Use.Startup() 2 次,這就是我沒有通過的原因。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/422125.html
標籤:
