.NET C # IIS Request aborted: Could not create a secure channel for SSL / TLS

6

Srs,

I'm trying to communicate with the recipe server / SEFAZ using an A1 certificate through an MVC5 C # application on an IIS 8 server, in the pool with my user's identity. The certificate is installed and I have already performed the tests by connecting directly to the recipe from the Browser. However when doing the request in the code I get the following error:

The request was aborted: A secure channel for SSL / TLS could not be created.

The request data:

Parameters:

<xml version="1.0" encoding="UTF-8" >
 <consNFeDest versao="2.00 / 3.10" xmlns="http://www.portalfiscal.inf.br/nfe">
   <tpAmb>1</tpAmb>
   <cUF>31</cUF>
   <xServ>CONSULTAR NFE DEST</xServ>
    <CNPJ>000000000</CNPJ>
    <indNFe>0</indNFe>
   <indEmi>1</indEmi>
   <ultNSU>0</ultNSU>
</consNFeDest>
</xml>

Service: link Version: 2.00 / 3.10

Code:

var parametros = Serializar<ConsultaStatusDoServico>();
    var certificado = PegarCertificado(loja);
    var ws = new SefazMG.NfeStatusServico2();
    var cabecalho = new nfeCabecMsg() { cUF = Contexto.ConfiguracoesDaNFE.Estado.ID.ToString(), versaoDados = Contexto.ConfiguracoesDaNFE.VersaoDoServicoDeStatus };
    var xEle = new XmlDocument(); 
    xEle.LoadXml(parametros);
    XmlNode node = xEle.DocumentElement;
    ws.ClientCertificates.Add(certificado);
    ws.Url = "https://www.nfe.fazenda.gov.br/NFeConsultaDest/NFeConsultaDest.asmx";
    ws.nfeCabecMsgValue = cabecalho;
    var nfeDadosMsg = new nfeDadosMsg();
    nfeDadosMsg.Any = new XmlNode[] { xEle.ChildNodes.Item(0) };

    var sb = new StringBuilder();


    nfeDadosMsg.Any = new XmlNode[] { xEle };
    nfeDadosMsg.Any[0] = node;
    try
    {
        var resposta = ws.nfeStatusServicoNF2(nfeDadosMsg);
        sb.AppendLine("Resposta : " + Serializar(resposta));

    }catch(Exception ex)
    {
        sb.AppendLine(ex.Message);
    }

Error:

The request was aborted: A secure channel for SSL / TLS could not be created.

    
asked by anonymous 29.08.2015 / 00:19

4 answers

2

As unbelievable as it seems to be by the text of the error, the problem is due to the fact that, unlike other services of the same provider, it does not accept ContentType="application / xml", only the ContentType="text / xml ".

Thank you very much for your contribution.

    
03.11.2015 / 17:28
0
  

Request aborted: Could not create secure channel for   SSL / TLS.   this error is quite generic and has several things that can generate it ...

I already had a similar error but in my case it was because the server was not accepting or better validating the HTTPS certificate .. so I created it from here that solved my problem see if it can help you ...

private void AlgumLugar() {
    ServicePointManager.ServerCertificateValidationCallback 
    += new RemoteCertificateValidationCallback(AllwaysGoodCertificate);
}

private static bool AllwaysGoodCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors policyErrors) {

   return true;
}

add this here too, to work on windows 7 too !!:

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

Take a test and see if it works ... with me it worked!

    
29.08.2015 / 01:30
0

I've added the following code and it worked, but I'm not communicating with SEFAZ .

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11;
    
30.05.2018 / 19:52
0

Hello, I went through the same problem however trying to download a file from a website running in the background with the C # language. To download the file I use the GetResponse () method of the Web Client, the system worked perfectly until one day it gave an error: "The request was aborted: Could not create a secure channel for SSL / TLS"

I tried anyway quoted above and nothing worked, until I checked the version of the .net framework used, 4.5.2 . Then I switched to the 4.6 version, only this way I solved the problem. Re-running again on windows server 2016 and windows 10

Note: It is possible that with a future update of windows the application stops working.

I hope I have helped everyone.

    
11.12.2018 / 14:56