Kestrel:URL在浏览器中有效时,Invoke-WebRequest不起作用?

时间:2020-01-31 18:04:07

标签: asp.net visual-studio powershell asp.net-core kestrel-http-server

更新:

仅当使用Kestrel启动应用程序时,才会发生以下错误。

我已经为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();

我使用Visual Studio 2019创建了一个新的测试Asp.Net核心Web API项目(具有 Windows身份验证)。然后在Visual Studio中运行它,并弹出一个显示json返回的浏览器。

然后,我打开一个Powershell窗口并测试了URL。出现以下错误?

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

我找到了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...}

它仍然出现错误?

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

1 个答案:

答案 0 :(得分:0)

这个对我有用。我可以从网络api读取它。 尝试使用带有-Certificate $ cert作为参数的Invoke-RestMethod。

$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