5

I have a WPF application, which starts a ASP.NET core WEB API application.

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.

Configurations:

  1. Web API configuration:

enter image description here

  1. In 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 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.

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

3
  • 4
    Vote up for the clear description! Commented Jun 19, 2017 at 19:27
  • I think you need to configure somewhere which certificate should be used. Commented Jun 19, 2017 at 19:30
  • When I run the Web API application as a startup project it works fine and no certificate are required. Would it require one when initiated from inside of some WPF application? Commented Jun 19, 2017 at 19:34

2 Answers 2

3

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.

So to enable SSL on Kestrel you need to add a cert then pass it in when setting up 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();
Sign up to request clarification or add additional context in comments.

2 Comments

took me some time to generate a .pfx and configure it on the browser for the Postman. But worked straight away! Thanks! Is there a way to use IIS, if I want to, with this setup (from inside of WPF)?
Yeah you could. You would need to launch IIS from your WPF app and configure it to listen to port 44300 (or whatever you assign it to) and enable SSL. How to tho, is beyond my knowledge. Good luck.
0

Enforce HTTPS in ASP.NET Core
https://learn.microsoft.com/en-us/aspnet/core/security/enforcing-ssl?view=aspnetcore-6.0&tabs=visual-studio



Developing locally with ASP.NET Core under HTTPS, SSL, and Self-Signed Certs
https://www.hanselman.com/blog/developing-locally-with-aspnet-core-under-https-ssl-and-selfsigned-certs

Generate self-signed certificates with the .NET CLI
https://learn.microsoft.com/en-us/dotnet/core/additional-tools/self-signed-certificates-guide

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.