c# - 如何忽略 SSL 策略来执行 HTTPClient 请求?

标签 c# asp.net ssl-certificate

我试图忽略 ssl 策略以使用 HttpClient 发出请求,但从未调用 ServerCertificateValidationCallback。在 ASP.NET Core 应用程序中调用 ServerCertificateValidationCallback 的正确位置是什么?我把它放在 HttpClient using 之前。

注意:这个类在net462类库中。

代码:

 System.Net.ServicePointManager.ServerCertificateValidationCallback += 
 delegate (object sender,      
 System.Security.Cryptography.X509Certificates.X509Certificate certificate,
 System.Security.Cryptography.X509Certificates.X509Chain chain,
 System.Net.Security.SslPolicyErrors sslPolicyErrors)
 {
     return true; 
 };

 using (var client = new HttpClient())
 {
    client.BaseAddress = new Uri("https://myapi");
    ...

    var result = client.PostAsync(method, httpContent).Result;
    ...
 }

最佳答案

考虑这样尝试

//Handle TLS protocols
System.Net.ServicePointManager.SecurityProtocol =
    System.Net.SecurityProtocolType.Tls
    | System.Net.SecurityProtocolType.Tls11
    | System.Net.SecurityProtocolType.Tls12;

var client = new HttpClient();
client.BaseAddress = new Uri("https://myapi");
//...

var result = await client.PostAsync(method, httpContent);

关于c# - 如何忽略 SSL 策略来执行 HTTPClient 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44929616/

相关文章:

c# - System.webServer 具有无效的子元素 'monitoring'

c# - 通过值转换定义的属性进行 Linq 查询

c# - API + SQL 存储的 Azure 配置

c# - 使用嵌套的 div 而不是表或流来渲染 RadioButtonList

即使证书在浏览器中有效,Java 也无法验证证书

java - Android - 由 : java. security.cert.CertPathValidatorException 引起:未找到证书路径的信任 anchor

c# - 从方法调用两个 Web 服务而不相互阻塞的最佳方法

c# - 检查集合中的项目是否与任何其他项目匹配的 Linq 方法

c# - 在进行 Web API 调用时捕获异常

javax.net.ssl.SSLHandshakeException : certificate expired - Local or Remote?