.NET 6(Core)步步学-创建项目时忘记选择了“配置 HTTPS”,需要 HTTPS 怎么办?

作者:vkvi 来源:ITPOW(原创) 日期:2022-4-1

.NET Core 创建项目时,有一项“配置 HTTPS”,如果没有勾上,后来又需要 HTTPS 怎么办呢?

一、打开 Properties/launchSettings.json

二、在我们这个项目那一节,applicationUrl 那个属性上,增加一个 https 的 URL,注意不同的端口注意使用分号隔开

示例

{
  "$schema": "https://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:56252",
      "sslPort": 0
    }
  },
  "profiles": {
    "Server3": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "launchUrl": "weatherforecast",
      "applicationUrl": "http://localhost:5256;https://localhost:7256",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "weatherforecast",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

"applicationUrl": "http://localhost:5256;https://localhost:7256",

题外,上面有个 launchUrl,也就是说每次运行的时候,默认打开这个网址。

三、是否需要自动重定向?

即访问 HTTP,自动定向到 HTTPS,如果需要,打开 Program.cs,添加一句:

app.UseHttpsRedirection();

相关阅读


相关文章