簡體   English   中英

以編程方式完成時,http add sslcert失敗

[英]http add sslcert fails when done programmatically

我開發了一個自托管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手動添加證書時,當我運行我的代碼時,有些不同。 阻止添加路線的東西。

解決方案實際上很簡單 - 我也一直在努力解決這個問題,現在已經找到了解決方案。 如何手動添加證書與以編程方式添加的證書不同? 嗯,簡短的回答是將證書加載行更改為:

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

關鍵是最后一個參數,它告訴證書保存存儲在機器位置的私鑰,而不是用戶位置。 然后netsh命令可以找到私鑰,並且可以工作。

Paul Stovell在解釋性文本中找到了解決方案,並在將證書加載到商店時如何設置該標志。

現在,為什么我不能以編程方式執行netsh功能是另一回事......

我想我已修好了。

我試圖安裝的.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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM