Search code examples
asp.net-coresslwindows-servicesasp.net-core-webapi

Unable to open HTTPS urls from .net core Rest API hosted as windows service


I am new to .net core and hosting a .Net core5 RestAPI as a WindowsService. But the problem is I am able to open http links but https always giving error ERR_CONNECTION_REFUSED

When I launch it in debug mode using IIS Express, I can access both http and https, but when i publish and, create and start the windows service, only http links are working.

As you can see below, I also configured the URL's to use https with port no, No Luck!! But one more weird issue is after hosting it as a windows service I am only able to access these url's on 5000 port and not the port which i specified.

I have also installed the certificates on my localhost.

I spent kind of full day and also new to .net core, not sure what i am doing wrong, any help would be greatly appreciated.

ProgramClass :

 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();

launchSetting.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"
    }
  }
}

Startup.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();
            });
        }

Solution

  • i was able to resolve this by specifying my own certificate which can be used by Kestrel, Since i was using Windows Service to host my API so it was not detecting existing certificate, I used the tutorial here:

    https://www.humankode.com/asp-net-core/develop-locally-with-https-self-signed-certificates-and-asp-net-core

    but now i ran into another issue, the jwt authentication is giving issues...

    I will post back, if i find a solution to jwt.

    System.InvalidOperationException: Scheme already exists: Bearer

    update: I specified Use.Startup() 2 times and thats the reason i was not getting past.