2012-09-07 34 views
9

Possible Duplicate:
The request was aborted: Could not create SSL/TLS secure channelLa richiesta è stata interrotta: Impossibile creare SSL/TLS canale sicuro

Sto cercando di inviare una richiesta HTTP con un certificato lato client. Il file, in questo caso un file .p12. Tuttavia quando raggiunge la linea responseStream = httpRequest.GetRequestStream(); lancia una WebException: System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel.

Sto eseguendo il debug su IIS7.5 (su Windows 7), dove l'identità del pool di applicazioni è "LocalSystem".

Come posso risolvere questo problema?

 System.IO.Stream responseStream = null; 
     string errorString = string.Empty; 
     ; 
     string postData = string.Empty; 
     HttpWebRequest httpRequest = null; 
     System.Text.Encoding Encoding = new System.Text.UTF8Encoding(); 
     try 
     { 
      XmlDocument orderXml = new XmlDocument(); 
      orderXml.Load(@"c:\xmlfile.xml"); 
      postData = orderXml.InnerXml; 

      byte[] byte1 = Encoding.GetBytes(postData); 

      httpRequest = (HttpWebRequest)WebRequest.Create("https://testurl.com/SOAP_v1_0/"); 
      httpRequest.Method = "POST"; 
      httpRequest.Timeout = 9000; 
      httpRequest.KeepAlive = false; 
      httpRequest.ContentType = "text/xml; charset=" + "utf-8"; 
      httpRequest.ContentLength = byte1.Length; 

      X509Certificate2 certificate = new X509Certificate2(@"c:\file.p12", "password", X509KeyStorageFlags.Exportable); 
      X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine); 

      try 
      { 
       store.Open(OpenFlags.ReadWrite); 

       if (!store.Certificates.Contains(certificate)) 
       { 
        store.Add(certificate); 
       } 

       int indexOfCertificate = store.Certificates.IndexOf(certificate); 
       certificate = store.Certificates[indexOfCertificate]; 
      } 

      finally 
      { 
       store.Close(); 
      } 

      httpRequest.ClientCertificates.Add(certificate); 

      responseStream = httpRequest.GetRequestStream(); 

      responseStream.Write(byte1, 0, byte1.Length); 
     } 
     catch (WebException webExcp) 
     { 
      errorString += "Error message: " + webExcp.Message; 

      // Get the WebException status code. 
      WebExceptionStatus status = webExcp.Status; 

      if (status == WebExceptionStatus.ProtocolError) 
      { 
       // Get HttpWebResponse so that you can check the HTTP status code. 
       HttpWebResponse httpResponse = (HttpWebResponse)webExcp.Response; 
       errorString += "; The server returned protocol error " + httpResponse.StatusCode + " - " + httpResponse.StatusCode; 
       httpResponse.Close(); 
      } 
     } 
     catch (Exception e) 
     { 
      errorString += "Error message: " + e.Message; 
     } 
     finally 
     { 
      if (responseStream != null) 
      { 
       responseStream.Close(); 
      } 
     } 
    } 

Quando si esegue con una traccia di log sono queste le linee specificando l'errore:

System.Net Information: 0 : [4968] SecureChannel#2399524 - Certificate is of type X509Certificate2 and contains the private key. 

System.Net Information: 0 : [4968] AcquireCredentialsHandle(package = Microsoft Unified Security Protocol Provider, intent = Outbound, scc  = System.Net.SecureCredential) 

System.Net Error: 0 : [4968] AcquireCredentialsHandle() failed with error 0X8009030D. 

System.Net Information: 0 : [4968] AcquireCredentialsHandle(package = Microsoft Unified Security Protocol Provider, intent = Outbound, scc  = System.Net.SecureCredential) 

System.Net Error: 0 : [4968] AcquireCredentialsHandle() failed with error 0X8009030D. 
System.Net.Sockets Verbose: 0 : [4968] Socket#59311937::Dispose() 

System.Net Error: 0 : [4968] Exception in the HttpWebRequest#50160154:: - The request was aborted: Could not create SSL/TLS secure channel. 

System.Net Error: 0 : [4968] Exception in the HttpWebRequest#50160154::EndGetRequestStream - The request was aborted: Could not create SSL/TLS secure channel. 
+0

Puoi navigare con successo allo stesso URL con un browser? Questo stabilisce correttamente la sessione SSL/TLS? – Richard

+0

Non ricevo "Internet Explorer non può visualizzare la pagina web" – Rutger

+0

Quindi, guarda i dettagli tramite il link nella pagina di errore. È necessario sapere perché il canale SSL/TLS non può essere creato. Senza queste informazioni indoviniamo le impostazioni di configurazione su client e server. – Richard

risposta

31

È necessario creare un registro system.net per la vostra applicazione. Dovrai creare un file di configurazione myapp.exe.config e inserire quanto segue.

<?xml version="1.0" encoding="UTF-8"?> 

<configuration> 
    <system.diagnostics> 
     <trace autoflush="true" /> 
     <sources> 
      <source name="System.Net"> 
       <listeners> 
        <add name="System.Net" /> 
       </listeners> 
      </source> 
      <source name="System.Net.Sockets"> 
       <listeners> 
        <add name="System.Net" /> 
       </listeners> 
      </source> 
      <source name="System.Net.Cache"> 
       <listeners> 
        <add name="System.Net" /> 
       </listeners> 
      </source> 
     </sources> 
     <sharedListeners> 
      <add 
       name="System.Net" 
       type="System.Diagnostics.TextWriterTraceListener" 
       initializeData="System.Net.trace.log" 
      /> 
     </sharedListeners> 
     <switches> 
      <add name="System.Net" value="Verbose" /> 
      <add name="System.Net.Sockets" value="Verbose" /> 
      <add name="System.Net.Cache" value="Verbose" /> 
     </switches> 
    </system.diagnostics> 
</configuration> 

Se si esegue con questo file di configurazione, verrà creato un file di registro denominato system.net.trace.log. Quel file avrà maggiori dettagli sul perché questo sta fallendo.

+0

domanda aggiornata con la parte di errore del registro di traccia – Rutger

+0

Se non si desiderano messaggi di errore più dettagliati, aggiungere 'traceOutputOptions =" ​​DateTime, ProcessId, ThreadId, Callstack "' come attributi al 'sharedListener'' System.Net'. Come questo: ' ' – Ogglas

Problemi correlati