"Run as Administrator"(System.Net.WebException:请求已中止:无法创建 SSL/TLS 安全通道。)

"Run as Administrator" (System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel.)

我只在 2 台电脑上收到 WebException 错误(我尝试了 5 台不同的电脑)。 如果我启动应用程序 "As administrator".

则不会出现此问题

我已经尝试添加

ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;

并更改证书的不同标志

X509Certificate2 Cert = new X509Certificate2(fileName, "", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet);

但是没有用。

    try
    {
        string webLink = @"https://" + destinationIP + "/apps/MMC/";
        //get firmware version of trellisware radio
        string fileName = Path.GetDirectoryName(Application.ExecutablePath) + Path.DirectorySeparatorChar + "etc" + Path.DirectorySeparatorChar + "default.p12";
        X509Certificate2 Cert = new X509Certificate2(fileName, "", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet);
        CertificateWebClient myWebClient = new CertificateWebClient(Cert);
        string webData = myWebClient.DownloadString(webLink);
        Uri responseUri = myWebClient.ResponseUri;

        string[] response = responseUri.ToString().Split('/');
    }
    catch (WebException ws) 
    { 
        logger.Error("WebException: " + webLink + Environment.NewLine + ws);
    }
    catch (SocketException) { }
    catch (ThreadAbortException) { }
    catch (Exception ex)
    {
        logger.Error("Exception: " + webLink + Environment.NewLine + ex);
    }

    public class CertificateWebClient : WebClient
    {
        private readonly X509Certificate2 certificate;

        public CertificateWebClient(X509Certificate2 cert)
        {
            certificate = cert;
        }

        protected override WebRequest GetWebRequest(Uri address)
        {
            HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);

            System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate (Object obj, X509Certificate X509certificate, X509Chain chain, System.Net.Security.SslPolicyErrors errors)
            {
                return true;
            };

            request.ClientCertificates.Add(certificate);
            return request;
        }

        Uri _responseUri;

        public Uri ResponseUri
        {
            get { return _responseUri; }
        }

        protected override WebResponse GetWebResponse(WebRequest request)
        {
            WebResponse response = base.GetWebResponse(request);
            _responseUri = response.ResponseUri;
            return response;
        }
    }

例外情况:

2019-05-13 07:04:48.0050 | Error | OManager | WebException: https://169.1.28.23/apps/MMC/
System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel.
   at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
   at System.Net.WebClient.DownloadString(Uri address)
   at CAstralPilot.OcelotManager.<>c__DisplayClass26_0.<getGCSTrelliswareVersion>b__0() in 


2019-05-13 07:04:48.2081 | Warn | Genesys.Bayeux.Client | Request transport failed. Retrying after 00:00:00 Exception: 
Genesys.Bayeux.Client.BayeuxTransportException: Unable to connect to the remote server ---> System.Net.WebSockets.WebSocketException: Unable to connect to the remote server ---> System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel.
   at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Net.WebSockets.ClientWebSocket.<ConnectAsyncCore>d__21.MoveNext()
   --- End of inner exception stack trace ---
   at System.Net.WebSockets.ClientWebSocket.<ConnectAsyncCore>d__21.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Genesys.Bayeux.Client.WebSocketTransport.<Open>d__12.MoveNext()
   --- End of inner exception stack trace ---
   at Genesys.Bayeux.Client.WebSocketTransport.<Open>d__12.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Genesys.Bayeux.Client.ConnectLoop.<Poll>d__16.MoveNext()

我希望代码可以在所有 PC 上运行,为什么其中一些需要管理员权限才能连接 WebClient? 我是否需要在某些受信任的位置添加证书才能正常工作?

解决方案是在 管理用户证书 (certmgr) 和 管理计算机证书 (certlm)