Search code examples
c#asp.net-coreasp.net-core-mvcvisual-studio-2022

How to run ASP.NET Core MVC web page on localhost port 443


I am using Visual Studio 2022 and I created an ASP.NET Core MVC web app in .NET 6.0. I can change the settings to use a non https URL and it works. This page will eventually need to run on https in our QA and Production environments. I do not use IIS.

If I configure launchsettings.json to use https, port 443, I get an error. When I try to run the page locally using the URL https://localhost:44337:

The connection for this site is not secure
localhost sent an invalid response.
Try running Windows Network Diagnostics.
ERR_SSL_PROTOCOL_ERROR

This is my launchSettings.json file:

{
  "profiles": {
    "MicroGraphicsMVC": {
      "applicationUrl": "https://localhost:44337",
      "commandName": "Project",
      "dotnetRunMessages": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "launchBrowser": true,
      "sslPort": 44337
    }
  }
}

In my program.cs file, I have:

var builder = WebApplication.CreateBuilder(args);

////configure listening port
builder.WebHost.ConfigureKestrel(options => options.Listen(IPAddress.Any, 44337));

// Add services to the container.
builder.Services.AddControllersWithViews();

//add Dependency Injection for our objects
builder.Services.AddScoped<IMicroGraphics, MicroGraphicsRepo>();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
}

//app.UseHttpsRedirection();
//app.UseStaticFiles();

app.UseRouting();

//app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

How do I get my page to run locally on https?

Edit - Mr. Wang's advice almost works. I don't get the error any more, but now my CSS/formatting doesn't work. See the screenshot.

enter image description here


Solution

  • I replicated your issue in my side.

    enter image description here

    I found the issue is related to builder.WebHost.ConfigureKestrel and it would work if I comment this line. So I'm afraid it related with missing certificate. I found document here which demonstrate to bind a certficate, so I export the local self-signed certificate created by VS itself and put it in my app root content and have a test, and the exception disappearred.

    builder.WebHost.ConfigureKestrel((context, serverOptions) =>
    {
        serverOptions.Listen(IPAddress.Any, 44337, listenOptions =>
        {
            listenOptions.UseHttps("localhost.cer");
        });
    });
    

    enter image description here

    enter image description here

    Exporting the certificate by opening mmc, add/remove Snap-ins, Certificates, Computer account, click OK. Choose certificate -> Personal -> Certificates -> double click the localhost certificate -> choose details section -> copy to file.

    enter image description here