忽略错误的证书

编程入门 行业动态 更新时间:2024-10-22 20:25:41
本文介绍了忽略错误的证书 - .NET CORE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在编写一个 .NET Core 应用程序来轮询远程服务器并传输显示的数据.这在 PHP 中运行良好,因为 PHP 忽略了证书(这在浏览器中也是一个问题),但我们希望将其移至 C# .NET CORE,因为这是系统中唯一剩下的 PHP.

I'm writing a .NET Core app to poll a remote server and transfer data as it appears. This is working perfectly in PHP because the PHP is ignoring the certificate (which is also a problem in browsers) but we want to move this to C# .NET CORE because this is the only remaining PHP in the system.

我们知道服务器很好,但由于各种原因,证书无法/不会很快更新.

We know the server is good, but for various reasons the certificate can't / won't be updated any time soon.

请求正在使用 HttpClient:

The request is using HttpClient:

HttpClient httpClient = new HttpClient(); try { string url = "URLGoesHere.php"; MyData md = new MyData(); // this is some data we need to pass as a json string postBody = JsonConvert.SerializeObject(md); httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage wcfResponse = await httpClient.PostAsync(url, new StringContent(postBody, Encoding.UTF8, "application/json")); Console.WriteLine(wcfResponse.Content); } catch (HttpRequestException hre) { // This exception is being triggered }

对此进行研究后,似乎普遍建议使用 ServicePointManager,但这在 .NET Core 中不可用,我无法找到推荐的替代品.

Having researched this it seems the universal recommendation is to use ServicePointManager, but this is not available in .NET Core and I'm having trouble finding the recommended replacement.

在 .NET Core 中是否有一种简单或更好的方法来做到这一点?

Is there a simple or better way to do this in .NET Core?

推荐答案

你想要类似于 new HttpClient() 的东西

Instead of new HttpClient() you want something akin to

var handler = new System.Net.Http.HttpClientHandler(); using (var httpClient = new System.Net.Http.HttpClient(handler)) { handler.ServerCertificateCustomValidationCallback = (request, cert, chain, errors) => { // Log it, then use the same answer it would have had if we didn't make a callback. Console.WriteLine(cert); return errors == SslPolicyErrors.None; }; ... }

这应该可以在 Windows 和 Linux 上运行,其中 libcurl 被编译为使用 openssl.使用其他 curl 后端,Linux 会抛出异常.

That should work on Windows, and on Linux where libcurl is compiled to use openssl. With other curl backends Linux will throw an exception.

更多推荐

忽略错误的证书

本文发布于:2023-11-07 22:49:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1567651.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:证书   错误

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!