简体   繁体   English

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

[英]http add sslcert fails when done programmatically

I have developed a self-hosted api. 我开发了一个自托管api。

The api traffic needs to run over SSL. api流量需要通过SSL运行。

Using a combination of netsh commands I have managed to successfully add the certificate and then bind a route to my service. 使用netsh命令的组合我已成功添加证书,然后将路由绑定到我的服务。 Happy Days. 快乐的时光。

But, I have to write an installer to do this programmatically. 但是,我必须编写一个安装程序来以编程方式执行此操作。

The problem is that when I add the certificate using my c# code, I can see it the certificate MMC but when I try to bind to it I get an error of: 问题是,当我使用我的c#代码添加证书时,我可以看到证书MMC,但是当我尝试绑定到它时,我得到一个错误:

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

As I say, when I do it manually with these steps I don't get the problem... 正如我所说,当我用这些步骤手动完成时,我没有遇到问题......

  1. List item 项目清单
  2. Double click on the .pfx file. 双击.pfx文件。
  3. MMC opens. MMC打开。
  4. I select "Local Machine" 我选择“本地机器”
  5. On the next screen I confirm the .pfx file location and name. 在下一个屏幕上,我确认.pfx文件的位置和名称。
  6. I enter the password for the certificate and select "Include all extended properties" 我输入证书的密码,然后选择“包括所有扩展属性”
  7. On the next screen I let it default to "Automatically select the certificate store based on the type of certificate" 在下一个屏幕上,我让它默认为“根据证书类型自动选择证书存储”
  8. I then get a confirmation screen. 然后我得到一个确认屏幕。
  9. When I click "Finish" I get a message "The import was successful" 当我点击“完成”时,我收到一条消息“导入成功”

I can then see it in MMC under Personal > Certificates 然后我可以在个人>证书下的MMC中看到它

And it lets me add the route using netsh from a command prompt - Happy Days. 它允许我从命令提示符 - Happy Days使用netsh添加路由。

When I try an do it programmatically with the following code: 当我尝试使用以下代码以编程方式执行此操作时:

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; }
    }

The certificate appears in my MMC in exactly the same place but when I try and add the route with the exact same netsh command as before I get the error mentioned above: 证书出现在我的MMC中完全相同的地方,但是当我尝试使用与之前完全相同的netsh命令添加路由时,我得到上述错误:

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

For some reason, when I add the certificate manually using the MMC and when I run my code something is different. 出于某种原因,当我使用MMC手动添加证书时,当我运行我的代码时,有些不同。 Something that is stopping the route being added. 阻止添加路线的东西。

The solution is actually simple - I too have struggled with this, and have now found the solution. 解决方案实际上很简单 - 我也一直在努力解决这个问题,现在已经找到了解决方案。 How can a manually added certificate differ from the programatically added one? 如何手动添加证书与以编程方式添加的证书不同? Well, the short answer is to change your certificate load line to this: 嗯,简短的回答是将证书加载行更改为:

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

The key being that last parameter, which tells the certificate to save the private key stored in the machine location, and not the user location. 关键是最后一个参数,它告诉证书保存存储在机器位置的私钥,而不是用户位置。 Then the netsh command can find the private key, and can work. 然后netsh命令可以找到私钥,并且可以工作。

The solution was found in the explanatory text by Paul Stovell and some digging to see how I could set that flag when loading the certificate into the store. Paul Stovell在解释性文本中找到了解决方案,并在将证书加载到商店时如何设置该标志。

Now, why I can't programmatically do the netsh function is another matter... 现在,为什么我不能以编程方式执行netsh功能是另一回事......

I think I have fixed it. 我想我已修好了。

there was a problem with the .pfx I had been trying to install. 我试图安装的.pfx出现问题。 I have no idea why. 我不知道为什么。 what fixed it for me was exporting a working certificate from my personal store with all options set to true and then run it in the following: 为我修复的是从我的个人商店导出一个工作证书,所有选项都设置为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;
        }

    }

and then use that pfx file 然后使用该pfx文件

I have no idea why the old pfx worked from the command line but didn't work from code. 我不知道为什么旧的pfx从命令行工作但是没有从代码中工作。

HTH HTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过PowerShell脚本完成Visual Studio 17构建失败 - Visual studio 17 build fails when it is done by PowerShell script 当我以编程方式将其添加到UIstackview时,UIView不会隐藏 - UIView is not hiding when I add it programmatically to UIstackview 将RestSharp与Xamarin一起使用-HTTP请求失败时Execute()引发异常 - Using RestSharp with Xamarin - Execute() throws exceptions when HTTP request fails 尝试发布包含XML的JSON数据时Http发布失败 - Http Post Fails when Trying to Post JSON Data that contains XML 在单元测试中返回 FileContentResult 时,Http 响应标头解析器失败 - Http response headers parser fails when returning FileContentResult in unit test 以编程方式篡改http请求 - Tamper with http requests programmatically 外部身份验证失败时返回哪个HTTP状态? - Which HTTP Status to return when external authentication fails? 什么时候拍照? - When is taking a photo done? 如何在数据绑定时以编程方式向 datagridview 添加一行? - How to programmatically add a row to a datagridview when it is data-bound? 以编程方式添加到div时仅看不到我的某些控件 - Unable to see only some of my control when programmatically add to a div
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM