2016-08-17 25 views
0

我想實現的解決方案到我的應用程序,反映了答案in this post「專用密鑰」沒有一個定義,「CreateFromFile」

我有一個類似的場景,我有一個HttpListenerGrapevine基於應用程序上運行我需要獲得使用MonoHTTPS,我試圖創建幷包括相關按鍵的工作Ubuntu的服務器允許HTTPS

我遇到的問題是該解決方案的最後一行,

key = PrivateKey.CreateFromFile (pvk_file).RSA;

當我嘗試同樣的Visual Studio顯示錯誤/文本高亮紅,'PrivateKey' does not have a definition for 'CreateFromFile'

我使用了錯誤的庫或者是別的東西與我的代碼本身的問題?

我的代碼,減少到相關的方法。

using System; 
using System.IO; 
using System.Security.Cryptography.X509Certificates; 
using System.Threading; 
using java.security; 

public class ConfigureCertificates 
    { 
     private readonly string _dirName; 
     private readonly string _path; 
     private readonly string _port; 
     private readonly string _certFile; 
     public ConfigureCertificates(string port) 
     { 
      _dirName = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); 
      _path = Path.Combine(_dirName, ".mono"); 
      _path = Path.Combine(_path, "httplistener"); 
      _port = port; 
      _certFile = Path.Combine(_path, String.Format("{0}.cer", _port)); 
     } 

     public void SetUpCerts() 
     { 
      if (!File.Exists(_certFile)) 
       throw new Exception("Certificate file not found"); 

      string pvkFile = Path.Combine(_path, String.Format("{0}.pvk", _port)); 

      if (!File.Exists(pvkFile)) 
       throw new Exception("Private key not found"); 

      var cert = new X509Certificate2(_certFile); 
      var key = PrivateKey.CreateFromFile(pvkFile).RSA; // Error occurs here 
     } 
    } 
+1

嘗試使用完整的命名空間,或許這裏有類名的衝突。 'Mono.Security.Authenticode.PrivateKey.CreateFromFile()' – DavidG

+0

這就是現貨。謝謝。只需要添加來自Nuget的'Mono.Security' –

回答

1

你有一個命名衝突 - 換句話說,還有一種叫PrivateKey類,沒有有您所需要的方法。 A quick Google hunt表示正確的類位於Mono.Security.Authenticode命名空間中。所以,你需要引用的完整路徑:

Mono.Security.Authenticode.PrivateKey.CreateFromFile(...) 

您可能還需要添加Mono.Security包,如果你還沒有擁有它。

相關問題