我有一个Azure网站和一个用于存储.cer X509证书文件的Azure Blob。

目标是从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),则会因为输入不能处理url而出现错误。

知道我可以在Azure网站或Blob中用作临时存储并从中创建X509Certificate吗?

编辑:我正在尝试添加有关我要解决的问题的更多详细信息。
  • 从Azure Blob获取证书文件并将其写入Azure网站。
  • string pathToCert对象上使用.Import(X509Certificate)创建证书,该证书将用于以我在 Controller 中编写的方法进行调用。

  • 通过使用FTP将.cer文件手动添加到我的网站的wwwroot文件夹中,我已经能够解决1。但是现在当我使用Server.MapPath("~/testcert.cer");获取证书的路径时,我得到了:D:\home\site\wwwroot\testcert.cer
    显然,当Import方法将上面的字符串部署到我的azure网站后,将其用作路径时,它不是有效的路径,因此我的证书创建失败。

    有任何想法吗?谢谢!

    最佳答案

    在本地保存证书通常对于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);
    

    关于azure - 从Azure Blob保存X509证书并在Azure网站中使用它,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25697447/

    10-12 12:34
    查看更多