从Azure Blob保存X509证书并在Azure网站中使用它

Ord*_*r66 2 azure x509certificate2 azure-storage-blobs azure-web-sites

我有一个Azure网站和Azure Blob,我用它来存储.cer X509证书文件.

目标是从blob 获取.cer文件并使用它来执行操作(该代码在Controller我的Azure网站中,并且可以正常运行).

当我在本地运行代码(不发布我的网站)时,它可以工作,因为我保存了它 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);**
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚如何/在哪里可以保存它.如果我尝试使用Import方法但输入一个url(存储证书的Azure blob的URL),我收到一个错误,因为Import无法处理url.

知道我可以在Azure网站或blob中使用什么作为临时存储并X509Certificate从中创建一个?

编辑:我正在尝试添加有关我正在尝试解决的问题的更多详细信息.

  1. 从Azure blob获取证书文件并将其写入Azure网站.

  2. string pathToCertX509Certificate对象上使用.Import()来创建证书,该证书将用于在我在控制器中编写的方法中进行调用.

我已经能够.cer通过FTP 手动将文件添加到我的网站的wwwroot文件夹来解决1 .但是现在当我Server.MapPath("~/testcert.cer");用来获取证书的路径时,我得到了这个:D:\home\site\wwwroot\testcert.cer

显然,当Import方法将上面的字符串用作路径时,一旦将其部署到我的azure网站,它就不是有效路径,因此我的证书创建失败了.

有任何想法吗?谢谢!

Sea*_*eau 5

在本地保存证书对于Azure来说通常是禁止的,你已经获得了BlobStorage.

使用Import(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);
Run Code Online (Sandbox Code Playgroud)