2

https://我已经实现了一种使用 APS.NET 核心( )从 REST API 请求(基于 SSL 的请求:)获取响应的方法System.Net.HttpWebRequest

我需要忽略获取WebResponse. 我参考了许多博客并获得了使用ServicePointManager和使用以下代码的解决方案

ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true; 

但在 ASP.NET 核心中不支持ServicePointManager. 所以请帮助我解决这个问题,而HttpWebRequest不是通过HttpClient.

4

2 回答 2

4

.NET Core 使您可以覆盖ServerCertificateCustomValidationCallback委托。例子:

var handler = new HttpClientHandler();
handler.ServerCertificateCustomValidationCallback = delegate { return true; };
var http = new HttpClient(handler);
Console.WriteLine(http.GetAsync("https://expired.badssl.com/").GetAwaiter().GetResult());

输出:

StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Server: nginx/1.10.0
  Server: (Ubuntu)
  Date: Mon, 06 Mar 2017 05:56:52 GMT
  Transfer-Encoding: chunked
  Connection: keep-alive
  ETag: W/"58924b62-1d5"
  Cache-Control: no-store
  Content-Type: text/html
  Last-Modified: Wed, 01 Feb 2017 20:56:02 GMT
}

在带有 .NET 1.0.0-preview2-1-003177 的 Ubuntu 16.04 上进行了测试。在 MacOS 上,有一个与此相关的未解决问题。

于 2017-03-06T06:00:42.010 回答
-1

在调用请求之前:

ServicePointManager.ServerCertificateValidationCallback = new   
RemoteCertificateValidationCallback(validarCertificado);

...并包括此功能:

protected bool validarCertificado(Object sender,
                              X509Certificate certificado,
                              X509Chain cadena,
                              SslPolicyErrors sslErrores)
{
   return true;
}
于 2017-07-20T16:22:16.167 回答