【问题标题】:Kestrel: Invoke-WebRequest doesn't work while the URL works in browser?Kestrel:当 URL 在浏览器中工作时,Invoke-WebRequest 不起作用?
【发布时间】:2020-01-31 18:04:07
【问题描述】:

更新:

只有在使用 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

【问题讨论】:

  • 应该是http还是https?
  • 这可能只是隐藏了证书验证错误。
  • 我已经用 cert 参数更新了问题。
  • 你可以试试Invoke-RestMethod https://localhost:5001/weatherforecast -CertificateThumbprint 7A16573FF2DBA47695B8CA15916D445C9361F255 吗?
  • @CodeRunner, Invoke-RestMethod 得到同样的错误。

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


【解决方案1】:

这个对我有用。我可以从 web 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

【讨论】:

  • 使用-certificate 时仍然遇到同样的错误。我只是在 Visual Studio 中运行新创建的项目。
  • 现在 Invoke-RestMethod https://localhost:44334/weatherforecast -Certificate $cert -UseDefaultCredentials 在我使用 IIS Express 更改了 Visual Studio 后工作。(Kestrel 得到错误)
猜你喜欢
  • 1970-01-01
  • 2019-02-25
  • 1970-01-01
  • 2015-06-12
  • 1970-01-01
  • 2018-08-16
  • 2018-03-29
  • 2014-04-22
  • 1970-01-01
相关资源
最近更新 更多