23

I am following this tutorial to add Facebook authentication to my web app.

As part of the process I am trying to enable SSL on my project but everything I have found involves updating a setting in the Project Properties dialog in Visual Studio, which is unavailable to me through Visual Studio Code on my Mac. I've tried updating the values in launchSettings.json manually, but I haven't had any luck.

How do I update launchSettings.json (or other project files) in Visual Studio Code to enable SSL while debugging?

Pramod Gharu
  • 1,105
  • 3
  • 9
  • 18
Jeff Gardner
  • 301
  • 1
  • 2
  • 7
  • Related: [Configure ASP.NET Core 2.0 Kestrel for HTTPS](https://stackoverflow.com/q/46336341/216074) – poke Apr 16 '18 at 08:09

4 Answers4

19

If you don't want to change your Program.cs file just for debugging in VS Code, you can also configure the urls in your launch.json. You need to specify the urls in the env property. As xneg said you'll need to set up a self-signed cert to do this.

You can configure the http url and the https (SSL) url

"configurations":[
    {
        ...
        "env": {
            "ASPNETCORE_ENVIRONMENT": "Development",
            "ASPNETCORE_URLS": "http://localhost:5002;https://localhost:5003"
        },
        ...
    }

The documentation for Kestrel was helpful in figuring this out.

Brad Lawrence
  • 1,483
  • 1
  • 11
  • 11
  • 1
    this one must be the answer for today. wish I can't vote 10 times – d_f Apr 24 '19 at 16:54
  • 3
    Also, run from the VSCode terminal: `dotnet dev-certs https --trust` May also be preferable to trust the cert that will be used by Kestrel. [Documentation reference](https://learn.microsoft.com/en-us/aspnet/core/security/enforcing-ssl?view=aspnetcore-3.1&tabs=netcore-cli#trust-the-aspnet-core-https-development-certificate-on-windows-and-macos) – Benjamin Brandt Apr 07 '20 at 16:41
  • But it still open random port. Any idea? – Jack Nov 01 '20 at 17:06
10

I made the following edits to launchSettings.json on windows and it did the trick. Currently this is the only way to do it in Visual Studio 2017 RC .

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:50183/",
      "sslPort": 44318
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "https://localhost:44318",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "corePostgresIdentity": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://localhost:44318"
    }
  }
}
Sam Sippe
  • 3,160
  • 3
  • 27
  • 40
  • 1
    I updated my launchSettings.json to match yours, but the project is still starting on the original port 5000 rather than 50183. And if I put `https://localhost:44318` into the address bar, my browser says it cannot connect to the server. Not sure where it's getting the 5000 from, restarting VS Code hasn't helped. There must be some piece I am missing. – Jeff Gardner Dec 21 '16 at 16:18
  • I'm not sure, sorry. Maybe try to check out https://learn.microsoft.com/en-us/aspnet/core/tutorials/your-first-mac-aspnet . Also, search all the files in your solution for "5000", it must be in there somewhere. – Sam Sippe Dec 21 '16 at 23:56
  • 2
    For anyone else who might have this problem in the future - if you're also using WebListener (https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/weblistener) then it will listen on port 5000 and ignore your launchSettings.json configuration. – jacobappleton Jun 08 '17 at 20:25
  • Can these iisSettings be configured using cli ? – Shujath Feb 01 '21 at 13:00
  • How come the sslPort setting is different from the port listed in applicationUrl? Which one is leading? Why even have redundant properties in this setting file? In my project, the applicationUrl's port and the sslPort match. – Jay Jul 19 '21 at 12:23
  • @Jay It's been a while since I've looked at this but my guess is that in the example above 50183 is the http port and 44318 is the https port. – Sam Sippe Jul 21 '21 at 06:40
  • Thanks for the suggestion. No wonder I never found out, since my site launches on https and also forces a redirect towards that, when attempting to visit http. – Jay Jul 21 '21 at 13:49
2

When you run your ASP.NET Core app in VS Code you run it with Kestrel not IIS. You need to setup Kestrel to enable SSL manualy like this (in Program.cs):

public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .UseKestrel(options =>
            {
                options.Listen(IPAddress.Loopback, 5000, listenOptions =>
                {
                    listenOptions.UseHttps("localhost.pfx", "yourPassword");
                });
            })
            .UseUrls("https://localhost:5000")
            .Build();

How to create a self-signed certificate is described in this great article.

Alexander
  • 9,104
  • 1
  • 17
  • 41
xneg
  • 1,204
  • 15
  • 24
1

Usually when you modify the properties for your project, changes are persisted in launchSettings.json. So you need to change launchSettings.json like below:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:8837/",
      "sslPort": 0 //Add ssl port here
    }
  },
 "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "https://localhost:8837",
      "environmentVariables": {
      "ASPNETCORE_ENVIRONMENT": "Development"
     }
},
Rahul Nikate
  • 6,192
  • 5
  • 42
  • 54
  • See my comment on Sam Sippe's answer--same thing happened when I updated my launchSettings.json file to match yours. :-\ – Jeff Gardner Dec 21 '16 at 16:25