【问题标题】:Configure launchSettings.json for SSL in debug - ASP.NET Core / Visual Studio Code在调试中为 SSL 配置 launchSettings.json - ASP.NET Core / Visual Studio Code
【发布时间】:2016-12-21 05:04:05
【问题描述】:

我正在按照this 教程将 Facebook 身份验证添加到我的网络应用程序。

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

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

【问题讨论】:

标签: c# macos asp.net-core visual-studio-code


【解决方案1】:

如果您不想更改 Program.cs 文件只是为了在 VS Code 中进行调试,您还可以在您的 launch.json 中配置 url。您需要在 env 属性中指定 url。正如 xneg 所说,您需要设置一个自签名证书才能执行此操作。

您可以配置 http url 和 https (SSL) url

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

documentation for Kestrel 有助于解决这个问题。

【讨论】:

  • 这个一定是今天的答案。希望我不能投票 10 次
  • 另外,从 VSCode 终端运行:dotnet dev-certs https --trust 也最好信任 Kestrel 将使用的证书。 Documentation reference
  • 但它仍然打开随机端口。有什么想法吗?
【解决方案2】:

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

【讨论】:

  • 我更新了我的 launchSettings.json 以匹配你的,但该项目仍然从原始端口 5000 而不是 50183 开始。如果我在地址栏中输入https://localhost:44318,我的浏览器会说它不能连接到服务器。不确定从哪里获得 5000,重新启动 VS Code 并没有帮助。一定有一些我错过了。
  • 我不确定,抱歉。也许尝试查看docs.microsoft.com/en-us/aspnet/core/tutorials/…。此外,在解决方案中的所有文件中搜索“5000”,它一定在某处。
  • 对于将来可能遇到此问题的任何其他人 - 如果您还使用 WebListener (docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/…),那么它将侦听端口 5000 并忽略您的 launchSettings.json 配置。
  • 这些iisSettings可以用cli配置吗?
  • 为什么sslPort设置和applicationUrl中列出的端口不一样?哪个领先?为什么在这个设置文件中甚至有多余的属性?在我的项目中,applicationUrl 的端口和 sslPort 匹配。
【解决方案3】:

当您在 VS Code 中运行 ASP.NET Core 应用程序时,您使用 Kestrel 而不是 IIS 运行它。 您需要像这样(在 Program.cs 中)手动设置 Kestrel 以启用 SSL:

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 描述了如何创建自签名证书。

【讨论】:

    【解决方案4】:

    通常,当您修改项目的属性时,更改会保留在 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"
         }
    },
    

    【讨论】:

    • 查看我对 Sam Sippe 答案的评论——当我更新我的 launchSettings.json 文件以匹配您的文件时,也发生了同样的事情。 :-\
    最近更新 更多