Failed to determine the https port for redirect
Asked Answered
S

5

11

I am new to ASP.NET and programming too. I am using Visual Studio 2022 v17.5 and ASP.NET Core 6 for building a web application.

I am in the development environment. Initially all was going well but suddenly it starting misbehaving by showing

Warning: Failed to determine the https port for redirect

The web page loads but doesn't respond to any click and browser reload animation also keeps on revolving.

enter image description here

I searched the internet but failed to find any fix. Please guide to fix this error and thanks in advance for any solution.

Senary answered 13/3, 2023 at 5:21 Comment(2)
Have you tried to add "https_port": 443, in your appsetting.json?Deka
Sorry, it doesn't work. The response is "This site can't be reached".Senary
T
0

I had the same issue. Solved by deleting all .vs folder

Tega answered 16/3, 2023 at 11:21 Comment(1)
Unfortunately, after I had again the same issue later... trying to fix with VS repairTega
D
9

Here is an answer from MS Docs:

A port must be available for the middleware to redirect an insecure request to HTTPS. If no port is available:

  • Redirection to HTTPS doesn't occur.
  • The middleware logs the warning "Failed to determine the https port for redirect."

Specify the HTTPS port using any of the following approaches:

  • Set HttpsRedirectionOptions.HttpsPort.

  • Set the https_port host setting:

    • In host configuration.

    • By setting the ASPNETCORE_HTTPS_PORT environment variable.

    • By adding a top-level entry in appsettings.json:

      "https_port": 443,

  • Indicate a port with the secure scheme using the ASPNETCORE_URLS environment variable. The environment variable configures the server. The middleware indirectly discovers the HTTPS port via IServerAddressesFeature. This approach doesn't work in reverse proxy deployments.

  • The ASP.NET Core web templates set an HTTPS URL in
    Properties/launchsettings.json for both Kestrel and IIS Express.
    launchsettings.json is only used on the local machine.

  • Configure an HTTPS URL endpoint for a public-facing edge deployment
    of Kestrel server or HTTP.sys server. Only one HTTPS port is used by the app. The middleware discovers the port via
    IServerAddressesFeature.

Deka answered 13/3, 2023 at 6:44 Comment(3)
Thanks a lot for your contribution sir. I got my answer in https://mcmap.net/q/996585/-failed-to-determine-the-https-port-for-redirectSenary
I find this answer is much more concise and speaks to how to address the issue outside of development. When running the application in a deployed environment, I was getting this. So the current selected answer does not apply, because you don't have a .vs folder in a deployed environmentSalverform
Thank you! Simply setting the ASPNETCORE_HTTPS_PORT environment variable in my docker run command fixed this for me.Crus
A
3

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

Amphistylar answered 27/4 at 8:13 Comment(2)
Why hasnt this been liked more? This is an extensive and comprehensive answer, which outlines the issues and how to solve.Tibiotarsus
Thank you, Yenmangu for kind words - for me, it's enough ;-) Have a good day!Amphistylar
T
0

I had the same issue. Solved by deleting all .vs folder

Tega answered 16/3, 2023 at 11:21 Comment(1)
Unfortunately, after I had again the same issue later... trying to fix with VS repairTega
C
0

Heres something I found when I had the same problem. Set the Application Pool to "Load User Profile" -> TRUE. This is because IIS passes the HTTP port info in environment variables. It will probably work on your PC as its running in your profile, once IIS hosts it then its a different profile.

enter image description here

Cupreous answered 23/5, 2023 at 13:24 Comment(0)
S
0

I was using version .NET 7 and after installing the SignalR package, I encountered a problem.

The solution for me was to change the package whose last version was for .NET 8 to version 7. I hope it will be useful.

Stockade answered 25/1 at 7:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.