Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Azure Blob保存X509证书并在Azure网站中使用_Azure_Azure Storage Blobs_Azure Web App Service_X509certificate2 - Fatal编程技术网

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

从Azure Blob保存X509证书并在Azure网站中使用,azure,azure-storage-blobs,azure-web-app-service,x509certificate2,Azure,Azure Storage Blobs,Azure Web App Service,X509certificate2,我有一个Azure网站和一个Azure Blob,用于存储.cer X509证书文件 目标是从blob中获取.cer文件,并使用它执行操作(代码在my Azure网站的控制器中,可以正常工作) 当我在本地运行代码(不发布我的站点)时,它可以工作,因为我将代码保存在D:\ CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnection

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

目标是从blob中获取.cer文件,并使用它执行操作(代码在my 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);**
我想不出如何/在哪里保存它。如果我尝试使用导入方法,但输入了一个url(存储证书的Azure blob的url),我会收到一个错误,因为导入无法处理url

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

编辑:我正在尝试添加有关我试图解决的问题的更多细节

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

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

  • 我已经能够通过FTP将
    .cer
    文件手动添加到我站点的wwwroot文件夹中,从而解决1的问题。但是现在当我使用
    Server.MapPath(“~/testcert.cer”)
    要获取我的证书路径,我得到以下信息:
    D:\home\site\wwwroot\testcert.cer

    显然,当导入方法在部署到my azure网站后使用上面的字符串作为路径时,它不是有效的路径,因此我的证书创建失败


    有什么想法吗?谢谢

    非常简单。答案涵盖了所有和任何网络主机,而不仅仅是Azure

    首先,我强烈建议您不要在Web项目中静态地放置文件夹的路径!那么你能做的就是:

  • 使用
    Server.MapPath(“~/certs”)
    方法获取网站根文件夹中
    certs
    文件夹的物理路径
  • 确保没有人可以从外部世界访问此文件夹:
  • 通过在web.config中添加此附加的
    位置
    部分,您将阻止对此文件夹的任何外部访问。请注意,
    location
    元素必须是web.config文件中根
    configuration
    元素的直接后代

    更新关于如何将文件写入ASP.NET web项目的本地文件系统的非azure相关部分:

    var path = Server.MapPath("~/certs"); 
    using (var fileStream = System.IO.File.OpenWrite(path + "\testcert.cer")) 
    {
      // here use the fileStream to write
    }
    
    以及有关如何使用Blob Stroage client将Blob内容写入本地文件的完整示例代码:

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

    但是,@SeanCocteau有一个很好的观点和更简单的方法——只需使用
    MemoryStream

    本地保存证书对于Azure来说通常是不允许的,您已经有了BlobStorage

    使用导入(字节[])重载在内存中保留和加载证书。这里有一个快速手动编码的尝试

      // 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);
    

    现在,您可以通过门户上载证书,向站点添加应用程序设置,并在站点的证书存储中显示证书

    有关更多详细信息,请参阅此博客文章:

    谢谢你的回答!我知道我可以使用Server.MapPath(“~/certs”)从根文件夹获取证书。首先,我如何在那里保存证书?我应该将OpenWrite(“路径”)更改为什么?//将blob内容保存到文件中。使用(var fileStream=System.IO.File.OpenWrite(“D:/testcert.cer”){blockBlob.DownloadToStream(fileStream);}var path=Server.MapPath(“~/certs”);使用(var fileStream=System.IO.File.OpenWrite(path+“\testcert.cer”))查看此空间:Azure网站中的证书处理更好支持即将推出:)