0

Update:

The following error only occur when launching the application using Kestrel.

I already added the following two lines for Microsoft.AspNetCore.Authentication.Negotiate

// in public void ConfigureServices(IServiceCollection services)
services.AddAuthentication(NegotiateDefaults.AuthenticationScheme).AddNegotiate();

// in public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
app.UseAuthentication();

I created a new test Asp.Net core Web API project (with Windows authentication) using Visual Studio 2019. And run it in Visual studio and a browser popped up with json returns shown.

Then I open a Powershell window and tested the url. It got the following error?

PS C:\> Invoke-WebRequest https://localhost:5001/weatherforecast
Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.
At line:1 char:1
+ Invoke-WebRequest https://localhost:5001/weatherforecast
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
   eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

I found the ASP.NET Core HTTPS development certificate.

PS C:\> ls Cert:\CurrentUser\My | ? Subject -eq 'CN=localhost' | fl

Subject      : CN=localhost
Issuer       : CN=localhost
Thumbprint   : 7A16573FF2DBA47695B8CA15916D445C9361F255
FriendlyName : ASP.NET Core HTTPS development certificate
NotBefore    : 12/6/2019 4:45:04 PM
NotAfter     : 12/5/2020 4:45:04 PM
Extensions   : {System.Security.Cryptography.Oid, System.Security.Cryptography.Oid, System.Security.Cryptography.Oid,
               System.Security.Cryptography.Oid...}

And it still got the error?

PS C:\> Invoke-WebRequest https://localhost:5001/weatherforecast -CertificateThumbprint 7A16573FF2DBA47695B8CA15916D445C -UseDefaultCredentials
9361F255
Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.
At line:1 char:1
+ Invoke-WebRequest https://localhost:5001/weatherforecast -Certificate ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
   eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
ca9163d9
  • 27,283
  • 64
  • 210
  • 413

1 Answers1

0

This one worked for me. I am able to read it from the web api. Give it try with Invoke-RestMethod with -Certificate $cert as parameter.

$cert = Get-ChildItem -Path Cert:\CurrentUser\My\4E1E5F6B5BAD49B13521C2DF12275C37E126DBB9
$response = Invoke-RestMethod https://localhost:5001/weatherforecast -Certificate $cert
Write-Host $response.Count

enter image description here

Kundan
  • 1,394
  • 1
  • 13
  • 26
  • I still get the same error by using `-certificate`. I just run the newly created project in Visual Studio. – ca9163d9 Jan 31 '20 at 21:22
  • 1
    Now `Invoke-RestMethod https://localhost:44334/weatherforecast -Certificate $cert -UseDefaultCredentials` works after I change the visual studio using IIS Express.(Kestrel got the error) – ca9163d9 Jan 31 '20 at 21:25