Failed to start ASP.NET Core with HTTPS

I have a WPF application that runs an ASP.NET Web API application.

When I run the WEB API project as a startup project with these configurations, it works fine for HTTPS. But, when I try to run this application from WPF environment, it doesn't work for HTTPS.

Configurations:

  • Web API configuration:

enter image description here

  1. In the Startup.cs file:
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:

enter image description here

Postman says:

enter image description here

Neither the action method debugger is hit in the application.

PS: When I check in changes for HTTPS and try to use HTTP, it works fine.

The main HTTP method has a different port and none of the configuration changes mentioned above.

+3


source to share


1 answer


When you enable SSL in your web server settings, you are enabling SSL for IIS, not your application. When you run the Web API from Visual Studio, it runs behind IIS as a reverse proxy service. This is why you only get SSL when you run it as a startup project. When you run it from your WPF app, the API only works in Kestrel.

So, to enable SSL in Kestrel, you need to add a certificate and then transfer it when configuring 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();

      

+2


source







All Articles