2015-09-11 28 views
5

Sto costruendo una console C# app che verrà:Certificato SSL add fallito, errore: 1312

[1.] generare un certificato auto-firmato.

[2.] Aggiungere al Personal (Local Computer Store)

[3] E infine assegnare tale certificato a un numero di porta sulla macchina con il comando netsh.

Finora, ho avuto parti [1.] e [2.] perfettamente funzionante, ma su [3.] sto afflitto con il messaggio di errore inutile e non informali:

SSL Certificate add failed, Error: 1312 A specified logon session does not exist. It may already have been terminated.

Quando vado un'occhiata alla pagina ufficiale di Microsoft su questo problema: https://support.microsoft.com/en-us/kb/981506

e 'fondamentalmente mi dice che si tratta di un bug di sistema operativo Windows e che avrei dovuto richiedere un hotfix

mio Hack soluzione a questo problema:

Un modo ho potuto finalmente bypassare questo errore, è stato di apertura IIS Home> Apri "certificati server" Feature> E poi Importazione mio certificato .pfx.

Importando il file .pfx in IIS, mi sembrava di essere in grado di risolvere il problema senza problemi. Ho solo bisogno di generare un pfx eseguendo entrambi questi due comandi per

1.) C:\OpenSSL-Win32\bin>openssl req -x509 -sha256 -nodes -days 365 -subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=www.example.com" -newkey rsa:2048 -keyout privateKey.key -out certificate.crt

2.) C:\OpenSSL-Win32\bin>openssl pkcs12 -export -out cert.pfx -inkey privateKey.key -in certificate.crt -passout pass:

Quindi, se corro quei due comandi di OpenSSL in questo momento, e l'importazione tramite IIS al mio archivio di certificati Computer locale personale, non avrò alcun problema SSL Certificate add failed, Error: 1312.

Tuttavia, se aggiungo il certificato appena generato in modo programmatico all'archivio certificati del computer locale personale, viene visualizzato il problema Errore 1312.

Ecco il mio codice:

using CERTENROLLLib; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Security.Cryptography.X509Certificates; 
using System.Text; 
using System.Threading.Tasks; 

using System.IO; 
using System.Text; 
using System.Security.Cryptography; 
using System.Diagnostics; 

namespace Generate_X509_Certificate 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 

      Console.WriteLine(Guid.NewGuid().ToString()); 
      Console.ReadLine(); 

      // Launch OpenSSL: 
      string cPath = @"C:\OpenSSL-Win32\bin\"; 
      string filename = Path.Combine(cPath, @"openssl.exe"); 

      // Generate a .crt file 
      Console.WriteLine(@"Generating SSL Certificate..."); 
      ProcessStartInfo startInfo = new ProcessStartInfo(@"C:\OpenSSL-Win32\bin\openssl.exe", @"req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout privateKey.key -out certificate.crt -subj ""/C=US/ST=California/L=SanFrancisco/CN=SecurityEncryption"""); 
      Process.Start(startInfo); 

      // Combine the .crt with the .key to form a more Windows friendly .pfx 
      Console.WriteLine(@"Combining Private Key With Certificate..."); 
      Process proc2 = Process.Start(filename, "pkcs12 -export -out cert.pfx -inkey privateKey.key -in certificate.crt -passout pass:"); 
      proc2.Close(); 

      // Store our newly created .pfx file as a variable 
      X509Certificate2 cert = new X509Certificate2(Directory.GetCurrentDirectory()[email protected]"\cert.pfx"); 

      // Add our .pfx file into the Personal Local Computer store: 
      var store = new X509Store(StoreLocation.LocalMachine); 
      store.Open(OpenFlags.ReadWrite); 
      store.Add(cert); 
      store.Close(); 

      // Finally, use netsh to assign this newly generated certificate to port 6613 
      string s1 = "netsh http add sslcert ipport=0.0.0.0:6613 certhash=‎‎‎" + cert.GetCertHashString() + " appid={" + Guid.NewGuid().ToString() + "}"; 
      Process p1 = new Process(); 
      p1.StartInfo.FileName = "netsh.exe"; 
      p1.StartInfo.Arguments = s1; 
      p1.StartInfo.UseShellExecute = false; 
      p1.StartInfo.RedirectStandardOutput = true; 

      // this is where I get the error "SSL Certificate add failed, Error: 1312" 
      p1.Start(); 
     } 
    } 
} 

Ecco il comando netsh che funziona perfettamente bene finché non sto eseguendo in questo programma C#:

netsh http add sslcert ipport=0.0.0.0:6613 certhash=‎‎‎‎8834efd403687b331363ce9e5657ba4ffba0075b appid={e604f84f-e666-4ccf-af52-fdeca666b5e9}

La parte confusa

Quindi, se si dovessero eseguire testualmente i miei comandi openssl da questa discussione, importare lo .pfx file generato da openssl in IIS e infine utilizzare il comando netssh come visto sopra con l'hash del certificato corretto, che l'intera cosa funziona perfettamente. Ma quando fai ciò che ho appena detto automaticamente nel codice C# sopra, ottengo l'errore.

Un'altra cosa, il.generato da pfx che importato nello store da questo codice non funzionerà affatto quando si tenta di eseguire manualmente netsh tramite la riga di comando.

Qualcuno ha qualche idea?

+0

Non è necessario eseguire netsh nel codice, come PInvoking HTTP API è molto più facile. –

risposta

2

provare esplicito includono la pfx per il CERT prima di negozio che se si utilizza C#

var store = new X509Store(StoreName.My, StoreLocation.LocalMachine); 
      store.Open(OpenFlags.ReadWrite); 
    //ensure pfx in cert. 
    byte[] pfx = cert.Export(X509ContentType.Pfx); 
    cert = new X509Certificate2(pfx, (string)null, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet); 

    //then store 
    store.Add(cert); 
    store.Close(); 
Problemi correlati