【问题标题】:ASP.NET Core: develop locally with custom base URL and httpsASP.NET Core:使用自定义基本 URL 和 https 在本地开发
【发布时间】:2019-03-31 21:58:27
【问题描述】:

我正在使用 ASP.NET Core。 Stackoverflow 上已经有与使用自定义 URL 进行开发相关的问题/答案,但我的情况似乎有点不同,因为我需要 https 支持和自定义基本 URL。我正在通过 OAuth 重定向流程向第 3 方授权。第 3 方仅支持 https 而非 localhost 的重定向 URL。开箱即用,Visual Studio 2017(版本 15.7.2)配置了 https 支持,这是一个惊喜。默认情况下,我的应用会在 https://localhost:44348/ 上启动。

launchSettings.json 看起来像这样:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:53837",
      "sslPort": 44348
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "MyProject.Web": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

我添加了一个主机条目以将自定义 url 映射到 127.0.0.1

127.0.0.1       app.mysite.local

...但我无法通过https://app.mysite.local:44348/ 访问我的网站。我收到 Bad Request - Invalid Hostname 错误。

我已尝试将 .vs/config/applicationhost.config 中的此条目更改为:

<binding protocol="https" bindingInformation="*:44348:localhost" />

<binding protocol="https" bindingInformation="*:44348:*" />

但是即使我以管理员身份运行 Visual Studio,我的应用程序也根本无法运行。 Visual Studio 显示此对话框:

我什至尝试过在IWebHostBuilder 上使用UseUrls() 扩展方法:

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .UseUrls("https://*:44348/");
}

...但是这些技术似乎都不起作用。在开发 ASP.NET Core 应用程序时,任何人都可以了解如何获取自定义基本 URL 以在 https 上工作吗?

【问题讨论】:

  • 您能否从您的启动设置中提供更多信息,我猜您那里也有个人资料。也猜测它不应该是一个问题,但你为什么不在 applicationUrl 中使用 app.mysite.local 而不是 localhost?自从您对主机文件进行更新后,您是否重新启动过。我记得这是一个潜在的问题。
  • @eVolve 我用完整的launchSettings.json 更新了我的帖子。是的,那里有个人资料。我尝试将 applicationUrl 更改为 app.mysite.local,但我收到了与上面的屏幕截图相同的 Invalid URI: The hostname could not be parsed 错误消息。我不认为是主机文件没有更新,因为 URL 似乎正确路由到应用程序。

标签: visual-studio asp.net-core


【解决方案1】:

已使用更新的 url 运行此程序,但缺少证书,这可能是接下来要查看的内容。当然,我知道这不是完整的答案,但我认为最好提供一些帮助,因为我目前没有时间尝试通过证书问题。

尝试将.vs/config/applicationhost.config 更改为以下内容(您需要将端口更新为您正在使用的端口),因此查看我的配置中的绑定通知,我在两个地方进行了更新:

      <site name="WebSite1" id="1" serverAutoStart="true">
    <application path="/">
      <virtualDirectory path="/" physicalPath="%IIS_SITES_HOME%\WebSite1" />
    </application>
    <bindings>
      <binding protocol="http" bindingInformation=":8080:localhost" />
      <binding protocol="https" bindingInformation="*:44335:app.mysite.local" />
    </bindings>
  </site>
  <site name="WebApplication2" id="2">
    <application path="/" applicationPool="Clr4IntegratedAppPool">
      <virtualDirectory path="/" physicalPath="C:\Users\benl\source\repos\WebApplication2\WebApplication2" />
    </application>
    <bindings>
      <binding protocol="http" bindingInformation="*:63297:localhost" />
      <binding protocol="https" bindingInformation="*:44335:app.mysite.local" />
    </bindings>
  </site>

另外,我以管理员身份运行 VS。我确实遇到了 IIS 错误,所以我确实重新启动了。应用程序在 localhost url 上加载并抱怨错误的请求。所以现在输入你的网址https://app.mysite.local:44335/(再次更改端口),它现在正在运行:

下一部分您需要为 IIS express 排序一个新证书,因为它很可能只为 localhost 设置。 Link to possibly how to do this

这也可能有用Link to cert creation / use for localdev

【讨论】:

  • 太棒了,它有效!看起来您在WebSite1 中添加了app.mysite.local 绑定,但我认为没有必要。似乎只有WebApplication2 中的那个是必要的。如果除了新的 https app.mysite.local 绑定之外,还保留旧的 https localhost 绑定,您将能够在 https://localhost:44335https://app.mysite.local:44335 上为该站点提供服务。遗憾的是,在添加 app.mysite.local 绑定的那一刻,您需要以管理员身份运行 Visual Studio,否则 IIS 将无法启动。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-25
  • 2020-07-11
  • 2011-01-06
  • 1970-01-01
  • 2016-12-22
  • 1970-01-01
相关资源
最近更新 更多