8

I am using Visual Studio 2017 and .NET Core 2.x. I have set up two projects. The first is the backend API (https://localhost:51637). The second is the front end website (https://localhost:54146). The front end website calls the back end API. They are both running locally on my machine.

Problem is, when the front end tries to send a request to the API, I get this exeption

info: Microsoft.AspNetCore.Server.Kestrel[20]
    Connection id "0HLKNTHFLCEA9" request processing ended abnormally.
System.IO.IOException: The decryption operation failed, see inner exception. ---> System.ComponentModel.Win32Exception: An unknown error occurred while processing the certificate
    --- End of inner exception stack trace ---
    at System.Net.Security.SslStreamInternal.ReadAsyncInternal[TReadAdapter](TReadAdapter adapter, Memory`1 buffer)
    at Microsoft.AspNetCore.Server.Kestrel.Core.Adapter.Internal.AdaptedPipeline.ReadInputAsync(Stream stream)
    at System.IO.Pipelines.PipeCompletion.ThrowLatchedException()
    at System.IO.Pipelines.Pipe.GetReadResult(ReadResult& result)
    at System.IO.Pipelines.Pipe.ReadAsync(CancellationToken token)
    at System.IO.Pipelines.Pipe.DefaultPipeReader.ReadAsync(CancellationToken cancellationToken)
    at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection.BeginRead(ValueTask`1& awaitable)
    at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)
    at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequestsAsync[TContext](IHttpApplication`1 application)
Kevin Brydon
  • 10,374
  • 6
  • 32
  • 66

2 Answers2

16

This arcane error may also occur between two .NET Core applications under development where .NET app is your self-hosted website or API and the other is a .NET client console app or whatever.

The server-side error is as per OP dump above, and the client-side error is:

System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception. ---> System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.

Run this to trust the development certificate and resolve the problem.

dotnet dev-certs https -t -v

More information here.

https://www.hanselman.com/blog/DevelopingLocallyWithASPNETCoreUnderHTTPSSSLAndSelfSignedCerts.aspx

Luke Puplett
  • 36,042
  • 37
  • 161
  • 231
6

Turns out, the API certificate wasn't trusted by my browser (Firefox). To fix this

  1. Create a blank "index.html" page in the API project wwwroot folder (make sure the API project is set up to serve static files)
  2. Run the API project
  3. Point the browser to the index.html page
  4. A warning will be displayed saying "Your connection is not secure"
  5. Click "Advanced" and "Add exception"

The front end website will now be able to call the back end API successfully.

Kevin Brydon
  • 10,374
  • 6
  • 32
  • 66