在调试中为SSL配置launchSettings.json - ASP.NET Core / Visual Studio Code

问题描述 投票:13回答:4

我正在关注this教程,将Facebook身份验证添加到我的网络应用程序中。

作为流程的一部分,我尝试在我的项目中启用SSL,但我发现的所有内容都涉及更新Visual Studio中“项目属性”对话框中的设置,我无法通过Mac上的Visual Studio代码使用该设置。我已经尝试手动更新launchSettings.json中的值,但我没有运气。

如何在Visual Studio代码中更新launchSettings.json(或其他项目文件)以在调试时启用SSL?

c# macos asp.net-core visual-studio-code
4个回答
6
投票

我在windows上对launchSettings.json进行了以下编辑,它就是诀窍。目前这是在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"
    }
  }
}

2
投票

如果您不想仅仅为了在VS Code中进行调试而更改Program.cs文件,还可以在launch.json中配置URL。您需要在env属性中指定URL。正如xneg所说,你需要设置一个自签名证书才能做到这一点。

您可以配置http网址和https(SSL)网址

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

documentation for Kestrel有助于搞清楚这一点。


2
投票

在VS Code中运行ASP.NET Core应用程序时,使用Kestrel而不是IIS运行它。您需要设置Kestrel来像这样手动启用SSL(在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();

如何创建一个自签名证书在这个伟大的article中描述。


0
投票

通常在修改项目的属性时,更改将保留在launchSettings.json中。所以你需要像下面这样改变launchSettings.json

{
  "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"
     }
},
© www.soinside.com 2019 - 2024. All rights reserved.