Configure launchSettings.json for SSL in debug - ASP.NET Core / Visual Studio Code

房东的猫 提交于 2020-12-04 17:15:05

问题


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?


回答1:


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.




回答2:


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



回答3:


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.




回答4:


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"
     }
},


来源:https://stackoverflow.com/questions/41255447/configure-launchsettings-json-for-ssl-in-debug-asp-net-core-visual-studio-co

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!