2

我有一個Azure網站和一個Azure Blob,我正用它來存儲.cer X509證書文件。從Azure Blob保存X509證書並在Azure網站中使用它

目標是從blob獲取.cer文件並使用它執行操作(該代碼位於我的Azure網站的Controller中,並且工作正常)。

當我在本地運行的代碼(不發表我的網站),它的作品,因爲我將它保存在D:\

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]); 

// Create the blob client. 
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); 

// Retrieve reference to a previously created container. 
CloudBlobContainer container = blobClient.GetContainerReference("myContainer"); 

// Retrieve reference to a blob named "testcert.cer". 
CloudBlockBlob blockBlob = container.GetBlockBlobReference("testcert.cer"); 

// Save blob contents to a file. 
using (var fileStream = System.IO.File.OpenWrite("D:/testcert.cer")) 
{ 
    blockBlob.DownloadToStream(fileStream); 
} 


**string certLocation = "D:/testcert.cer"; 
X509Certificate2 myCert = new X509Certificate2(); 

myCert.Import(certLocation);** 

我無法弄清楚如何/在那裏我可以保存它。如果我嘗試使用Import方法,但輸入一個url(存儲證書的Azure blob的url),我得到一個錯誤,因爲Import無法處理url。

任何想法,我可以用作在Azure網站或blob的臨時存儲,並從它創建一個X509Certificate

編輯:我想添加更多關於我試圖解決的問題的細節。

  1. 從Azure blob獲取證書文件並將其寫入Azure網站。

  2. 使用X509Certificate對象創建將被用來在我寫在我的控制器的方法的調用證書上.IMPORT(string pathToCert)。

我已經能夠通過手動添加.cer文件通過FTP我的網站的wwwroot文件夾來解決1。但現在當我使用Server.MapPath("~/testcert.cer");來獲取路徑獲取我的證書我得到這個:D:\home\site\wwwroot\testcert.cer

顯然,當導入方法使用上面,因爲一旦它部署到我的蔚藍網站的路徑字符串,它不是一個有效的路徑,因此我證書創建失敗。

任何想法?謝謝!

+0

關注此空間:在Azure的網站進行交接證書更好地支持即將推出:) – 2014-09-08 18:26:28

回答

0

很簡單。答案涵蓋了所有和任何網絡託管商,而不僅僅是Azure。

首先,我強烈建議你永遠不要靜態地把你的Web項目中的文件夾的路徑!那麼你可以做的是:

  1. 用戶Server.MapPath("~/certs")方法來獲得你的網站的根目錄文件夾內的文件夾certs的物理路徑。
  2. 確保沒有人可以從外界訪問此文件夾:

Location Deny content

通過在你的web.config添加此附加location節中,您是阻止此文件夾的任何外部訪問。請注意,location元素必須是web.config文件中根configuration元素的直接後裔。

UPDATE關於如何寫文件到ASP.NET Web項目的本地文件系統的非蔚藍的相關部分:

var path = Server.MapPath("~/certs"); 
using (var fileStream = System.IO.File.OpenWrite(path + "\testcert.cer")) 
{ 
    // here use the fileStream to write 
} 

,並就如何使用斑點Stroage客戶一個完整的示例代碼寫一個blob的內容到本地文件:

var path = Server.MapPath("~/certs"); 
using (var fileStream = System.IO.File.OpenWrite(path + "\testcert.cer")) 
{ 
    blockBlob.DownloadToStream(fileStream); 
} 

但是,@SeanCocteau好點的更簡單的方法 - 只要使用MemoryStream,而不是!

+0

感謝您的回答!我知道我可以使用Server.MapPath(「〜/ certs」)從根文件夾獲取證書。我如何在首位保存證書?我應該將OpenWrite(「路徑」)更改爲? //將blob內容保存到文件中。 (var fileStream = System.IO.File.OpenWrite(「D:/testcert.cer」)) { blockBlob.DownloadToStream(fileStream); } – Order66 2014-09-06 09:07:04

+2

var path = Server.MapPath(「〜/ certs」); using(var fileStream = System.IO.File.OpenWrite(path +「\ testcert.cer」)) – 2014-09-07 11:43:53

4

在本地保存證書通常是Azure的禁止,您已爲此獲取BlobStorage。

使用導入(byte [])過載來保存和加載證書在內存中。這裏有一個快速的手工編碼的嘗試......

// Used to store the certificate data 
    byte[] certData; 
    // Save blob contents to a memorystream. 
    using (var stream = new MemoryStream()) 
    { 
     blockBlob.DownloadToStream(stream); 
     certData = stream.ToArray(); 
    } 

    X509Certificate2 myCert = new X509Certificate2(); 
    // Import from the byte array 
    myCert.Import(certData);