Vojtěch Dohnal
Vojtěch Dohnal

Reputation: 8104

HTTP Error 400 running ASP.NET Core 2.1 React application

I am developing ASP.NET Core 2.1 React application in Visual Studio 2017. Today I wanted to launch a previously launchable application from Visual Studio, but the error appeared:

Bad Request - Invalid Hostname HTTP Error 400. The request hostname is invalid.

After I had switched from IIS Express to project exe in the launch menu in VS 2017, I observed the following message:

Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware[0]

The host 'localhost:5001' does not match an allowed host.

The whole log looks like this:

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]
      > [email protected] 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.

Any ideas what is behind and how to fix it?

I am suspicious about AVG antivirus making big wreckage lately or some update.

Upvotes: 1

Views: 6635

Answers (3)

Jeffrey LeCours
Jeffrey LeCours

Reputation: 1473

Ran into this today. Opened up my appsettings.json file to add the "AllowedHost": "localhost" line but found I already had an entry with

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)'

Just letting others out there like my know that you're not crazy. :-)

Upvotes: 1

Dbl
Dbl

Reputation: 5894

I've fixed this issue for me by adding this to the 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"
    }
}

Looks like you have to bind to the network ip in order to be able to connect to that ip without connection errors. at least i can reach that ip with no port forwarding from my phone after starting the server like this.

Upvotes: 0

Vojtěch Dohnal
Vojtěch Dohnal

Reputation: 8104

Ouch, I have played with AllowedHosts settings the other day and forgot about it. In appsetings.json file there is a line that should read for localhost:

"AllowedHosts": "localhost"

The valid IP address of the locahost does not work.

Upvotes: 9

Related Questions