【问题标题】:HTTP Error 400 running ASP.NET Core 2.1 React application运行 ASP.NET Core 2.1 React 应用程序的 HTTP 错误 400
【发布时间】:2019-02-11 12:08:17
【问题描述】:

我正在 Visual Studio 2017 中开发 ASP.NET Core 2.1 React 应用程序。今天我想从 Visual Studio 启动以前可启动的应用程序,但出现错误:

错误请求 - 无效主机名 HTTP 错误 400。请求主机名是 无效。

在 VS 2017 的启动菜单中从 IIS Express 切换到项目 exe 后,我观察到以下消息:

Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware[0]

主机 'localhost:5001' 与允许的主机不匹配。

整个日志如下所示:

info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
      User profile is available. Using 'C:\Users\vdohnal\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
info: Microsoft.AspNetCore.SpaServices[0]
      Starting create-react-app server on port 50222...
Hosting environment: Development
Content root path: C:\VSTS\Dixie\Dixie
Now listening on: https://localhost:5001
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
dbug: HttpsConnectionAdapter[1]
      Failed to authenticate HTTPS connection.
System.IO.IOException: Authentication failed because the remote party has closed the transport stream.
   at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
   at System.Net.Security.SslStream.BeginAuthenticateAsServer(SslServerAuthenticationOptions sslServerAuthenticationOptions, CancellationToken cancellationToken, AsyncCallback asyncCallback, Object asyncState)
   at System.Net.Security.SslStream.<>c.<AuthenticateAsServerAsync>b__51_0(SslServerAuthenticationOptions arg1, CancellationToken arg2, AsyncCallback callback, Object state)
   at System.Threading.Tasks.TaskFactory`1.FromAsyncImpl[TArg1,TArg2](Func`5 beginMethod, Func`2 endFunction, Action`1 endAction, TArg1 arg1, TArg2 arg2, Object state, TaskCreationOptions creationOptions)
   at System.Threading.Tasks.TaskFactory.FromAsync[TArg1,TArg2](Func`5 beginMethod, Action`1 endMethod, TArg1 arg1, TArg2 arg2, Object state, TaskCreationOptions creationOptions)
   at System.Threading.Tasks.TaskFactory.FromAsync[TArg1,TArg2](Func`5 beginMethod, Action`1 endMethod, TArg1 arg1, TArg2 arg2, Object state)
   at System.Net.Security.SslStream.AuthenticateAsServerAsync(SslServerAuthenticationOptions sslServerAuthenticationOptions, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Server.Kestrel.Https.Internal.HttpsConnectionAdapter.InnerOnConnectionAsync(ConnectionAdapterContext context)
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET https://localhost:5001/
info: Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware[0]
      The host 'localhost:5001' does not match an allowed host.
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 55.2128ms 400 text/html
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET https://localhost:5001/favicon.ico
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET https://localhost:5001/favicon.ico
info: Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware[0]
      The host 'localhost:5001' does not match an allowed host.
info: Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware[0]
      The host 'localhost:5001' does not match an allowed host.
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 6.4203ms 400 text/html
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 8.9463ms 400 text/html
info: Microsoft.AspNetCore.SpaServices[0]
      > Dixie@0.1.0 start C:\VSTS\Dixie\Dixie\ClientApp
> rimraf ./build && react-scripts start

Starting the development server...

info: Microsoft.AspNetCore.SpaServices[0]

Compiled with warnings.

info: Microsoft.AspNetCore.SpaServices[0]

./src/components/Home.js

info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET https://localhost:5001/
info: Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware[0]
      The host 'localhost:5001' does not match an allowed host.
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 3.6957ms 400 text/html
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET https://localhost:5001/
info: Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware[0]
      The host 'localhost:5001' does not match an allowed host.

任何想法背后是什么以及如何解决它?

我怀疑 AVG 杀毒软件最近或某些更新会造成严重破坏。

【问题讨论】:

    标签: reactjs visual-studio-2017 asp.net-core-2.1


    【解决方案1】:

    哎呀,前几天我玩过AllowedHosts 设置,然后忘记了。在appsetings.json 文件中有一行应该为 localhost 读取:

    “AllowedHosts”:“本地主机”

    本地主机的有效 IP 地址不起作用。

    【讨论】:

    • 这个答案让我省了很多心! =) 非常感谢!
    【解决方案2】:

    我已经通过将它添加到 launchSettings.json 为我解决了这个问题

    "ShutdownTimer.Server": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "ShutdownTimer.Server (localIP)": {
        "commandName": "Project",
        "launchBrowser": true,
        "applicationUrl": "https://192.168.0.115:5001;http://192.168.0.115:5000",
        "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
        }
    }
    

    看起来您必须绑定到网络 ip 才能连接到该 ip 而不会出现连接错误。在像这样启动服务器后,至少我可以在没有端口转发的情况下通过手机访问该 IP。

    【讨论】:

      【解决方案3】:

      今天遇到了这个。打开我的 appsettings.json 文件以添加 "AllowedHost": "localhost" 行,但发现我已经有了一个带有

      的条目
      info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
            Request starting HTTP/1.1 GET https://localhost:5001/
      info: Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware[0]
            The host 'localhost:5001' does not match an allowed host.
      info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
            Request finished in 2951.4396ms 400 text/html
      
      info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
            Request starting HTTP/1.1 GET https://localhost:5001/
      info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
            Executing endpoint 'AsyncControllerTest.Controllers.HomeController.Index (AsyncControllerTest)'
      

      只是让像我这样的人知道你没有疯。 :-)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-18
        • 1970-01-01
        • 2018-11-04
        • 1970-01-01
        • 2019-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多