ASP.NET Core redirect http to https but I don't want to do it in localhost also I use a different port

Viewed 1957

In ,net core 2.1, I want to redirect from http to https.

services.AddHttpsRedirection(options =>
   {
    options.RedirectStatusCode = StatusCodes.Status301MovedPermanently;
    options.HttpsPort = 5001;
    });

However in localhost I don't use https, I just use http. Also I use a different port.

"MyProject": {
  "commandName": "Project",
  "launchBrowser": true,
  "applicationUrl": "http://localhost:50642",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }

It doesn't work in localhost unless I change the port to 5001 in launchSettings.json. How to make it working?

2 Answers

if you don't want to redirect http > https in development, then in your Configure method use an if around app.UseHttpsRedirection();

if (appSettings.HttpsPort.HasValue)
{
    app.UseHttpsRedirection();
}

Just change the port as

"applicationUrl": "https://localhost:5001",

When you click F5 to debug, Visual Studio 2017 will ask you to install a cert for you automatically. You don't have to manually to do it.

Related