以编程方式完成时,http add sslcert失败

问题描述 投票:5回答:2

我开发了一个自托管api。

api流量需要通过SSL运行。

使用netsh命令的组合我已成功添加证书,然后将路由绑定到我的服务。快乐的时光。

但是,我必须编写一个安装程序来以编程方式执行此操作。

问题是,当我使用我的c#代码添加证书时,我可以看到证书MMC,但是当我尝试绑定到它时,我得到一个错误:

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

正如我所说,当我用这些步骤手动完成时,我没有遇到问题......

  1. 项目清单
  2. 双击.pfx文件。
  3. MMC打开。
  4. 我选择“本地机器”
  5. 在下一个屏幕上,我确认.pfx文件的位置和名称。
  6. 我输入证书的密码,然后选择“包括所有扩展属性”
  7. 在下一个屏幕上,我将其默认为“根据证书类型自动选择证书存储”
  8. 然后我得到一个确认屏幕。
  9. 当我点击“完成”时,我收到一条消息“导入成功”

然后我可以在个人>证书下的MMC中看到它

它允许我从命令提示符 - Happy Days使用netsh添加路由。

当我尝试使用以下代码以编程方式执行此操作时:

public static bool ConfigureSSLCertificate(string file, string password, string method)
    {
        try
        {
            X509Certificate2 cert = new X509Certificate2(file, password);

            var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
            store.Open(OpenFlags.ReadWrite);
            if (!store.Certificates.Contains(cert))
            {
                if (method == "add")
                {
                    store.Add(cert);
                }
            }
            if (method == "remove")
            {
                store.Remove(cert);
            }
            return true;
        }
        catch { return false; }
    }

证书出现在我的MMC中完全相同的地方,但是当我尝试使用与之前完全相同的netsh命令添加路由时,我得到上述错误:

netsh>http add sslcert ipport=0.0.0.0:8088 certhash=fb93ce2c4d8bd88c82e63e3372a050ba84f15e94 appid={bb14356a-a14f-4589-82ce-b80d38b8741e}

出于某种原因,当我使用MMC手动添加证书时,当我运行我的代码时,有些不同。阻止添加路线的东西。

c# ssl
2个回答
1
投票

解决方案实际上很简单 - 我也一直在努力解决这个问题,现在已经找到了解决方案。如何手动添加证书与以编程方式添加的证书不同?嗯,简短的回答是将证书加载行更改为:

X509Certificate2 cert = new X509Certificate2(file, password, X509KeyStorageFlags.MachineKeySet);

关键是最后一个参数,它告诉证书保存存储在机器位置的私钥,而不是用户位置。然后netsh命令可以找到私钥,并且可以工作。

Paul Stovell在解释性文本中找到了解决方案,并在将证书加载到商店时如何设置该标志。

现在,为什么我不能以编程方式执行netsh功能是另一回事......


0
投票

我想我已修好了。

我试图安装的.pfx出现问题。我不知道为什么。为我修复的是从我的个人商店导出一个工作证书,所有选项都设置为true,然后运行如下:

public static bool ServiceInstall(string serviceName, string serviceDescription, string executablePath)
    {
        try
        {
            ServiceProcessInstaller ProcesServiceInstaller = new ServiceProcessInstaller();
            ProcesServiceInstaller.Account = ServiceAccount.LocalSystem;

            ServiceInstaller ServiceInstallerObj = new ServiceInstaller();
            InstallContext Context = new System.Configuration.Install.InstallContext();
            String path = String.Format("/assemblypath={0}", executablePath);
            String[] cmdline = { path };

            Context = new System.Configuration.Install.InstallContext("", cmdline);
            ServiceInstallerObj.Context = Context;
            ServiceInstallerObj.DisplayName = serviceName;
            ServiceInstallerObj.Description = serviceDescription;
            ServiceInstallerObj.ServiceName = serviceName;
            ServiceInstallerObj.StartType = ServiceStartMode.Automatic;
            ServiceInstallerObj.Parent = ProcesServiceInstaller;

            System.Collections.Specialized.ListDictionary state = new System.Collections.Specialized.ListDictionary();
            ServiceInstallerObj.Install(state);
            return true;
        }
        catch
        {
            return false;
        }

    }

然后使用该pfx文件

我不知道为什么旧的pfx从命令行工作但是没有从代码中工作。

HTH

© www.soinside.com 2019 - 2024. All rights reserved.