Let's start from the beginning, assuming regular cases, how to set up HTTPS support - bear with me!
An ASP.NET project has the \Properties\launchSettings.json
file by default, which defines profiles for running the project with dotnet run
command. If you didn't change anything by yourself, you probably have three or four profiles. Two of them are http
and https
. And here our journey begins.
(BTW, you can rename these profiles if you really, really want, these are not fixed names.)
Names of these profiles are also visible in Visual Studio interface, in Run/Debug menus and toolbars. Visual Studio remembers the "default" selection in a user settings file, but the command line tool assumes, that default is the first profile in launchSettings.json
file.
So, back to our story, if you select e.g. http
profile in Visual Studio, or if you execute dotnet run
without switches, and the first profile is http
, then your application is not run with HTTPS port.
It can be misleading, because - I assume - you have configured your Program.cs
for supporting HTTPS by invoking this:
app.UseHttpsRedirection();
But this is not enough, this middleware only allows your project to use this redirection, it is not the request to run to do this.
This is the reason, why we have the warning "Failed to determine the https port for redirect" from Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware
because dotnet run
doesn't know, which port should be used. There is no default in such a case.
So, the first thing is to select the https
profile, by dropdown in Visual Studio, by reordering profiles in launchSettings.json
, or by using the switch
dotnet run --launch-profile https
.
Now it should work, but we can find another two troubles. (At least two.)
The numbers of HTTP or HTTPS ports are defined in profiles.
If the selected HTTPS port is not ready to use, because another application is using it, then our redirection will not be possible. How to check whether our HTTPS port, let's say 5001, is in use?
Test-NetConnection -ComputerName localhost -Port 5001
Then let's assume the port is available, the https
profile is selected, and when we start the program, we see such exception:
fail: Microsoft.Extensions.Hosting.Internal.Host[11]
Hosting failed to start
System.InvalidOperationException: Unable to configure HTTPS endpoint. No server certificate was specified, and the default developer certificate could not be found or is out of date.
To generate a developer certificate run 'dotnet dev-certs https'. To trust the certificate (Windows and macOS only) run 'dotnet dev-certs https --trust'.
For more information on configuring HTTPS see https://go.microsoft.com/fwlink/?linkid=848054.
This is because we forgot - or we did it, but a long time ago - about generating the certificate for our project with dev-cert
tool. The excellent introduction was made by Scott Hanselman, but to be short, we should use this:
dotnet dev-certs https --trust
Last note: the switch --trust
is important, because without it we will see a warning page like "Your connection to this site is not secure".
"https_port": 443,
in your appsetting.json? – Deka