Fix HttpClient’s SSL certificate verification issue in Windows 8 apps
In Windows 8 apps, there may be challenges when dealing with untrusted server certificates when using HttpClient to establish an SSL encrypted connection. Unlike WebRequest, HttpClient lacks the ServerCertificateValidationCallback functionality, limiting the ability to bypass certificate validation.
Meeting challenges
To solve this problem, a solution can be implemented using the .NET Standard library as follows:
<code class="language-csharp">// 创建自定义 HTTP 客户端处理程序 HttpClientHandler handler = new HttpClientHandler(); // 将客户端证书选项设置为手动 handler.ClientCertificateOptions = ClientCertificateOption.Manual; // 定义自定义服务器证书验证回调 handler.ServerCertificateCustomValidationCallback = (httpRequestMessage, certificate, chain, policyErrors) => true; // 使用自定义处理程序创建 HttpClient HttpClient client = new HttpClient(handler);</code>
By setting ServerCertificateCustomValidationCallback to return true, validation will be effectively bypassed. This allows HttpClient to trust all certificates presented, although this has the inherent security risk of accepting untrusted certificates.
The above is the detailed content of How Can I Bypass SSL Certificate Validation with HttpClient in Windows 8 Apps?. For more information, please follow other related articles on the PHP Chinese website!