zs
2024-11-21 aee9f67e702f6467aa1948777251fa1bf85353ec
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Security;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
 
namespace DataCapture_MA.Util
{
    public static class SSLUtil
    {
        /// <summary>
        /// Sets the cert policy.
        /// </summary>
        public static WebRequestHandler SetCertificatePolicy2()
        {
            var handler = new WebRequestHandler();
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
            handler.ServerCertificateValidationCallback += RemoteCertificateValidate;
            return handler;
        }
 
        /// <summary>
        /// Sets the cert policy.
        /// </summary>
        public static HttpClientHandler SetCertificatePolicy3()
        {
            HttpClientHandler clientHandler = new HttpClientHandler();
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
            //clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
            return clientHandler;
        }
 
        /// <summary>
        /// Sets the cert policy.
        /// </summary>
        public static void SetCertificatePolicy()
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
            ServicePointManager.ServerCertificateValidationCallback
                       += RemoteCertificateValidate;
        }
 
        /// <summary>
        /// Remotes the certificate validate.
        /// </summary>
        private static bool RemoteCertificateValidate(
           object sender, X509Certificate cert,
            X509Chain chain, SslPolicyErrors error)
        {
            // trust any certificate!!!
            System.Console.WriteLine("Warning, trust any certificate");
            return true;
        }
    }
}