【问题标题】:Problems with ASP.NET Core site using http.sys and Microsoft Edge使用 http.sys 和 Microsoft Edge 的 ASP.NET Core 站点的问题
【发布时间】:2018-08-02 20:27:46
【问题描述】:

我在启动和运行 ASP.NET Core 2.1 Web 应用程序时遇到了很多问题。我需要它在共享端口(80 或 443)上的 http.sys(WebListener)下运行。我还希望它自动从 http (80) 重定向到 https (443)。当然,我不想硬编码 http.sys 的侦听器地址——我需要从配置文件中提取它们,但现在它们是硬编码的。我使用netsh 保留了相应的 URL,但是当我运行应用程序时,我收到了警告:

warn: Microsoft.AspNetCore.Server.HttpSys.MessagePump[0]
      Overriding address(es) 'http://sharedhost.vbcoa.com:80/app/, https://sharedhost.vbcoa.com:443/app/'. Binding to endpoints added to UrlPrefixes instead.

应用程序启动,但我根本无法使用 Microsoft Edge 浏览它。任何其他网络浏览器都可以——只要我禁用 HTTPS。出于某种原因,应用程序正在转发到端口 5001,而不是 443。

【问题讨论】:

    标签: asp.net-core microsoft-edge http.sys


    【解决方案1】:

    我想通了这一切。有四个问题。我将逐一介绍它们。

    • 配置 http.sys 时,会发出关于覆盖本地 URL 的警告

    IWebHostBuilderUseHttpSys 扩展方法接受带有 UrlPrefixes 属性的选项参数。但是,这不是您应该配置 URL 的地方 - 即使您使用的是 http.sys。可以用IWebHostBuilderUseUrls扩展方法硬编码,但最好从配置中拉出来,这就引出了第二个问题:

    • 应从 appsettings.json 读取配置

    要指定要在哪些 URL 上运行应用程序,请将它们添加到 appsettings.json 中的“urls”元素中,如下所示:

    {
      "Logging": {
        "LogLevel": {
          "Default": "Warning"
        }
      },
      "AllowedHosts": "*",
      "urls": "http://sharedhost.vbcoa.com:80/app/;https://sharedhost.vbcoa.com:443/app/"
    }
    

    然后您需要创建一个ConfigurationBuilder 对象,将appsettings.json 文件添加到其中,构建配置(使用Build 方法)并告诉IWebHostBuilder 使用该配置,使用@987654333 @扩展方法:

    public static void Main(string[] args)
    {
        var configBuilder = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json");
    
        var hostBuilder = WebHost.CreateDefaultBuilder(args)
            .UseConfiguration(configBuilder.Build())
            .UseHttpSys()
            .UseStartup<Startup>();
    
        hostBuilder.Build().Run();
    }
    
    • 重定向到端口 5001,而不是 443

    HTTPS 重定向在 StartupConfigure 方法中指定 - 该功能开箱即用。但是,默认情况下,它将转发到端口 5001,即使您在上面的绑定 URL 中指定了另一个端口。要覆盖它,您需要通过服务注入 HTTPS 重定向选项。这是在StartupConfigureServices 方法中处理的。将以下行添加到该方法:

    services.AddHttpsRedirection(options => { options.HttpsPort = 443; });
    
    • Microsoft Edge 不会显示 Web 应用,即使其他所有浏览器都会显示

    这是 Windows 应用商店应用程序中 localhost 环回隔离的问题。它似乎会影响 Windows 10 企业版,如下所述:Microsoft Edge is not able to recognize localhost。要纠正它,您需要做两件事:

    1. 确保在 Edge 的“about:flags”页面中选中“Allow localhost loopback”。
    2. 以管理员身份启动命令提示符或 Powershell 提示符并输入以下内容:

      CheckNetIsolation LoopbackExempt -a -n=Microsoft.MicrosoftEdge_8wekyb3d8bbwe

    应该这样做!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-28
      • 2015-09-04
      • 1970-01-01
      相关资源
      最近更新 更多