I have a WPF application, which starts a ASP.NET core WEB API application.
我有一个WPF应用程序,它启动一个ASP。NET核心WEB API应用程序。
When I start the WEB API project as the startup project with these configurations, it works fine for HTTPS. But, when I try to launch this application from WPF environment, it's not working for HTTPS.
当我将WEB API项目作为具有这些配置的启动项目启动时,它可以很好地用于HTTPS。但是,当我尝试从WPF环境中启动这个应用程序时,它并不适用于HTTPS。
Configurations:
配置:
- Web API configuration:
- Web API配置:
- In Startup.cs file:
- 在启动。cs文件:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.Configure<MvcOptions>(options =>
{
options.Filters.Add(new RequireHttpsAttribute());
});
}
The Main method looks like this:
主要方法如下:
public static void InitHttpServer()
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseUrls("https://localhost:44300/")
//.UseApplicationInsights()
.Build();
host.Run();
}
When I check the port using netstat command, it shows:
当我使用netstat命令检查端口时,它显示:
Postman says:
邮递员说:
Neither the debugger on the action method in the application is being hit.
应用程序中操作方法上的调试器都没有被命中。
P.S. : When I revert the changes for HTTPS and try use HTTP, it works fine.
附注:当我恢复HTTPS的更改并尝试使用HTTP时,它工作得很好。
The main method for HTTP has different port and none of the configuration changes mentioned above.
HTTP的主要方法具有不同的端口,并且没有上面提到的任何配置更改。
1 个解决方案
#1
2
When you enable SSL in the web server settings your enabling SSL for IIS not your application. When you launch the web API from Visual Studio its running behind IIS as a reverse proxy service. This is why you get SSL only when you run it as the startup project. When you run it from your WPF application the API is running on Kestrel only.
在web服务器设置中启用SSL时,为IIS启用SSL而不是应用程序启用SSL。当您从Visual Studio启动web API时,它作为反向代理服务在IIS后面运行。这就是为什么只有在作为启动项目运行时才会获得SSL。当您从WPF应用程序运行它时,API仅在Kestrel上运行。
So to enable SSL on Kestrel you need to add a cert then pass it in when setting up Kestrel.
因此,要在Kestrel上启用SSL,您需要添加一个cert,然后在设置Kestrel时将其传入。
var cert = new X509Certificate2("YourCert.pfx", "password");
var host = new WebHostBuilder()
.UseKestrel(cfg => cfg.UseHttps(cert))
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseUrls("https://localhost:44300/")
//.UseApplicationInsights()
.Build();
#1
2
When you enable SSL in the web server settings your enabling SSL for IIS not your application. When you launch the web API from Visual Studio its running behind IIS as a reverse proxy service. This is why you get SSL only when you run it as the startup project. When you run it from your WPF application the API is running on Kestrel only.
在web服务器设置中启用SSL时,为IIS启用SSL而不是应用程序启用SSL。当您从Visual Studio启动web API时,它作为反向代理服务在IIS后面运行。这就是为什么只有在作为启动项目运行时才会获得SSL。当您从WPF应用程序运行它时,API仅在Kestrel上运行。
So to enable SSL on Kestrel you need to add a cert then pass it in when setting up Kestrel.
因此,要在Kestrel上启用SSL,您需要添加一个cert,然后在设置Kestrel时将其传入。
var cert = new X509Certificate2("YourCert.pfx", "password");
var host = new WebHostBuilder()
.UseKestrel(cfg => cfg.UseHttps(cert))
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseUrls("https://localhost:44300/")
//.UseApplicationInsights()
.Build();