SSL证书添加失败,错误:1312

本文关键字:错误 1312 失败 证书 添加 SSL | 更新日期: 2023-09-27 18:12:20

我正在构建一个c#控制台应用程序,它将:

[1。生成自签名证书。

[2。添加到个人(本地电脑商店)

[3。最后使用netsh命令将该证书分配给机器上的端口号。

到目前为止,我得到了部分[1]。[2]]工作完美,但在[3]。我被无用的和非正式的错误信息所困扰:

SSL证书添加失败,错误:1312指定的登录会话失败不存在的。它可能已经被终止。

当我去看微软关于这个问题的官方页面时:https://support.microsoft.com/en-us/kb/981506

它基本上是在告诉我这是一个Windows操作系统的错误,我应该请求一个热修复。

我的Hack解决方案:

我能够最终绕过这个错误的一种方法是打开IIS主页>打开"服务器证书"功能>然后导入我的。pfx证书。

通过导入。pfx到IIS,我似乎能够解决这个问题没有麻烦。我只需要按照

的顺序运行这两个命令来生成一个。pfx文件

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:

因此,如果我现在运行这两个命令到openssl,并通过IIS将它们导入到我的个人本地计算机证书存储,我将没有SSL Certificate add failed, Error: 1312问题。

但是,如果我以编程方式将新生成的证书添加到我的个人本地计算机证书存储中,那么我确实会得到Error:1312问题。

下面是我的代码:

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()+@"'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();
        }
    }
}

这是netsh命令,只要我不在这个c#程序中执行它就可以很好地工作:

netsh HTTP添加sslcert iport =0.0.0.0:66138834 efd403687b331363ce9e5657ba4ffba0075b certhash =‎‎‎‎appid = {e604f84f-e666-4ccf-af52-fdeca666b5e9}

令人困惑的部分

因此,如果您要从这个线程逐字执行我的openssl命令,然后将openssl生成的.pfx文件导入IIS,最后使用上面看到的netssh命令和适当的证书散列,那么整个事情就会完美地工作。但是当你在上面的c#代码中自动执行我刚才说的操作时,我得到了错误。

另一件事是,当您尝试通过命令行手动netsh时,从这段代码生成的.pfx将根本不能工作。

有人有什么想法吗?

SSL证书添加失败,错误:1312

如果使用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();