【问题标题】:HTTPs Client to connect to server without trusted chain (non trusted CA) on Azure Web AppHTTPS 客户端在 Azure Web App 上连接到没有受信任链(非受信任 CA)的服务器
【发布时间】:2018-09-29 06:35:09
【问题描述】:

我在尝试连接到具有尚未由受信任的 CA 签名的证书的服务器时被卡住了大约 3 天 - 您可以通过 Azure 门户添加 CA 的证书 - 但没有任何影响。

显然所有 HTTP 都使用内核模块 (!) HTTP.SYS。

我看到的所有建议都可以使用:

ServicePointManager.ServerCertificateValidationCallback = Communicator.CustomServiceCertificateValidation;

(或请求本身的不那么全局的对应物)

或类似的东西:

 client.ClientCredentials.ServiceCertificate.SslCertificateAuthentication =
                 new X509ServiceCertificateAuthentication()
                 {
                     CertificateValidationMode = X509CertificateValidationMode.Custom,
                     RevocationMode = X509RevocationMode.NoCheck,

                     //TrustedStoreLocation = StoreLocation.CurrentUser,
                     CustomCertificateValidator = new PermissiveCertificateValidator()

                 };

然而,两者都没有被调用!!

一些更重要的细节:

这行得通:

  try
        {
            ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
            using (var wc = new WebClient())
            {
                var res = wc.DownloadString("https://untrusted-root.badssl.com/");
                return Content(res);
            }
        }
        catch (Exception ex)
        {
            return Content(ex.ToString());
        }

但是,我需要使用客户端证书对自己进行身份验证 - 因此,请按照此处的说明进行操作: How can you add a Certificate to WebClient (C#)?

我最终得到以下代码:

class ATWebClient : WebClient
{
    protected override WebRequest GetWebRequest(Uri address)
    {
        HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
        request.Headers.Add("SOAPAction", "https://servicos.portaldasfinancas.gov.pt/sgdtws/documentosTransporte/");
        X509Certificate2 cert = new X509Certificate2();
        //From user installed Certificates
        //cert.Import(_pathCertificate, _passwordCertificate, X509KeyStorageFlags.DefaultKeySet);
        //From FileSystem "Resources\Certificates"
        cert.Import(Common.Properties.Resources.testewebservices_novo, "TESTEwebservice", X509KeyStorageFlags.Exportable);

        // Output Certificate 
        //Utils.Log(string.Format("Cert Subject: [{0}], NotBefore: [{1}], NotAfter: [{2}]", cert.Subject, cert.NotBefore, cert.NotAfter));

        request.ClientCertificates.Add(cert);
        request.Method = "POST";
        request.ContentType = "text/xml; charset=utf-8";
        request.Accept = "text/xml";


        return request;
    }

现在我调用服务:

   try
    {

        ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
        ServicePointManager.CheckCertificateRevocationList = false;
        ServicePointManager.UseNagleAlgorithm = false;
        using (var wc = new ATWebClient())
        {
            //var res = wc.DownloadString("https://servicos.portaldasfinancas.gov.pt:701/sgdtws/documentosTransporte");
            var res = wc.UploadString("https://servicos.portaldasfinancas.gov.pt:701/sgdtws/documentosTransporte", "Test of content");
            return Content(res);
        }


    }
    catch (Exception ex)
    {
        return Json(ex);
    }

注意,是的,这是一个 SOAP 端点 - 我尝试发送有效的 SOAP 内容,但两种情况下的结果相同,我得到以下异常:

The underlying connection was closed: An unexpected error occurred on a send.

堆栈跟踪:

   at System.Net.WebClient.UploadDataInternal(Uri address, String method, Byte[] data, WebRequest& request)
   at System.Net.WebClient.UploadString(Uri address, String method, String data)
   at System.Net.WebClient.UploadString(String address, String data)
   at MagniWebApp.Controllers.HomeController.NonTrustedCATestEndpoint() in C:\Users\joao.antunes.Office2\source\repos\07. Platform\WebApp\MagniWebApp\Controllers\HomeController.cs:line 1154

内部异常的消息:

Authentication failed because the remote party has closed the transport stream.

有什么想法吗?! 哈!

【问题讨论】:

    标签: c# ssl client ssl-certificate azure-web-app-service


    【解决方案1】:

    像这样在自定义回调中返回 true 的硬编码怎么样?

     public string Ssl()
        {
            try
            {
                ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
                using (var wc = new WebClient())
                {
                    var res = wc.DownloadString("https://untrusted-root.badssl.com/");
                    Console.WriteLine(res);
                    return res;
                }
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
    

    【讨论】:

    • 它甚至没有被调用!但是当我将不受信任的 CA 添加到商店时它会在本地执行 - 但是 - 这在 Azure Web 应用程序上不起作用!!
    • 我是否理解正确,您从 azure Web 应用程序调用不受信任的 https 服务器?尝试像这样在您的主机上安装它怎么样X509Certificate2Collection certificates = new X509Certificate2Collection(); certificates.Import(certName, password, X509KeyStorageFlags.PersistKeySet );
    • 是的!我做到了,@Artem Kurianov 它给出了一个未找到的文件 - 这与商店 AFAIK 有关 - 因为我实际上将证书(pfx)作为资源中的字节数组
    • 这很奇怪。在我的 azurewebapp 中它可以工作` public string Ssl() { try { ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true; using (var wc = new WebClient()) { var res = wc.DownloadString("untrusted-root.badssl.com/"); return res; } } catch (Exception ex) { return ex.ToString(); } }`
    • 终于进步了!!谢谢!因此,您的代码有效 - 这与我使用 HTTPWebRequest 的代码不同://Prepare Request HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_urlWebService); request.ServerCertificateValidationCallback += Communicator.CustomServiceCertificateValidation;现在我只需要弄清楚如何使用 WebClient 添加客户端证书和其他东西!有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-06
    • 1970-01-01
    • 2011-07-02
    • 2020-07-11
    • 2012-12-26
    相关资源
    最近更新 更多