.Net 6 提示SSL 证书验证错误(如:NotTimeValid等)参考解决方案

207 阅读1分钟

以使用HttpRequest为例:

            // Generate request message
            var httpRequest = new HttpRequestMessage(HttpMethod.Post, ApiUri);
            httpRequest.Headers.Add("host", Host);
            httpRequest.Content = new StringContent(Body,
                null,
                "application/json");

            // Create new handler with ignoring all certificate errors
            var handler = new HttpClientHandler();
            handler.ClientCertificateOptions = ClientCertificateOption.Manual;
            handler.ServerCertificateCustomValidationCallback =
                (httpRequestMessage, cert, cetChain, policyErrors) =>
                {
                    return true;
                };

            // Add handler into client
            var client = new HttpClient(handler);

            // Send reqeust
            var response = client.Send(httpRequest);
            Result = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();