首页 > 其他分享 >.NET问题:The remote certificate is invalid because of errors in the certificate chain: NotTimeValid

.NET问题:The remote certificate is invalid because of errors in the certificate chain: NotTimeValid

时间:2024-01-28 14:57:20浏览次数:28  
标签:because remote string certificate var new chain null

错误信息:

内部异常 1:
HttpRequestException: The SSL connection could not be established, see inner exception.

内部异常 2:
AuthenticationException: The remote certificate is invalid because of errors in the certificate chain: NotTimeValid

解决方法

跳过验证

                var httpClientHandler = new HttpClientHandler
                {
                    ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true,//绕过验证
                    ClientCertificateOptions = ClientCertificateOption.Manual
                };
                using var httpClient = new HttpClient(httpClientHandler);

 

全部代码

 1         /// <summary>
 2         /// post方法(带认证)
 3         /// </summary>
 4         /// <returns></returns>
 5         public static async Task<string> PostAsync(string serviceAddress, string requestJson = null, NameValueHeaderValue header = null, AuthenticationHeaderValue authenticationHeaderValue = null)
 6         {
 7             try
 8             {
 9                 string result = string.Empty;
10                 Uri postUrl = new Uri(serviceAddress);
11 
12                 var httpClientHandler = new HttpClientHandler
13                 {
14                     ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true,//绕过验证
15                     ClientCertificateOptions = ClientCertificateOption.Manual
16                 };
17 
18                 using (HttpContent httpContent = new StringContent(requestJson))
19                 {
20                     httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
21                     if (header != null)
22                     {
23                         httpContent.Headers.Add(header.Name, header.Value);
24                     }
25                     using var httpClient = new HttpClient(httpClientHandler);
26                     if (authenticationHeaderValue!= null)
27                     {
28                         httpClient.DefaultRequestHeaders.Authorization = authenticationHeaderValue;
29                     }
30                     
31                     httpClient.Timeout = new TimeSpan(0, 0, 60);
32                     result = await httpClient.PostAsync(serviceAddress, httpContent).Result.Content.ReadAsStringAsync();
33                 }
34                 return result;
35             }
36             catch (Exception e)
37             {
38                 throw new Exception(e.Message);
39             }
40         }
41 
42 
43 
44 
45         /// <summary>
46         /// 调用方法
47         /// </summary>
48         private async Tas Post()
49         {
50             var address = _configuration["Address"];
51             var loginName = _configuration["LoginName"];
52             var password = _configuration["PassWord"];
53 
54             // 设置认证信息 Basc Auth
55             string authCredentials = $"{loginName}:{password}";
56             string encodedAuthCredentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(authCredentials));
59             AuthenticationHeaderValue authHeaderValue = new AuthenticationHeaderValue("Basic", encodedAuthCredentials);
60             try
61             {
62                 var response= await PostAsync(address, JsonConvert.SerializeObject(request),null ,authHeaderValue);
64             }
65             catch (Exception ex)
66             {
67 
68             }
69         }
70 
71         

 

标签:because,remote,string,certificate,var,new,chain,null
From: https://www.cnblogs.com/chocolatexll/p/17992869

相关文章