Home > Net >  How to test UseHttpsRedirection setting locally in .NET Core
How to test UseHttpsRedirection setting locally in .NET Core

Time:02-06

In my ASP.NET Core project, I have turned ON HTTPS Redirection, with this setting in my Program.cs: app.UseHttpsRedirection();

I have referred to MS doc.

Locally, when I run my Core Web project, it uses https by default (https://localhost:7432/). Now, to test if redirection from http -> https works, I browse to http://localhost:7432/, but I get a "This page isn’t working right now" error.

So, how do I test if this redirection is working locally?

CodePudding user response:

In the Properties/launchSettings.json file, you'll see a property for applicationUrl that looks like this:

"applicationUrl": "https://localhost:7250;http://localhost:5097"

This sets up the app to listen on two different ports: 7250 for HTTPS and 5097 for HTTP. In your specific project, find the HTTP URL and use that to test the HTTPS redirection locally.

CodePudding user response:

As far I know, you can't share the same port for both http and https. Instead, you should define the ports in the appsettings.json or appsettings.Production.json. FYI, I never define such thing in appsettings.json, because that file is typically part of the repository. To me, the best place is in the other file.

In one of those files, you should see (or add whereas not present) something like this:

  "Kestrel": {
    "EndPoints": {
      "Http": {
        "Url": "http://*:5000"
      },
      "Https": {
        "Url": "https://*:5001",
        "Certificate": {
          "Path": "(your certificate file)",
          "Password": "(your certificate password)"
        },
      }
    }
  },
  "AllowedHosts": "*",
  "AllowInvalid": true,
  "https_port": 443,
  ...

Doing so, when you try to call your site as http, it will attempt to switch to the other port, using https.

For sure not the best explaination, but that's what I know about the https redirection.

  • Related