【问题标题】:Encryption with Azure Bob Storage v12 SDK for .Net使用适用于 .Net 的 Azure Blob Storage v12 SDK 进行加密
【发布时间】:2021-02-15 00:41:35
【问题描述】:

我想将我的代码迁移到v12 SDK,但是如何使用 Azure Keyvault?

没有 BlobEncryptionPolicy 类。

这个tutorial 已经过时了。它仍然基于旧的 SDK。

v11 SDK 代码:

// Retrieve the key that you created previously.
// The IKey that is returned here is an RsaKey.
var rsa = cloudResolver.ResolveKeyAsync(
            "https://contosokeyvault.vault.azure.net/keys/TestRSAKey1", 
            CancellationToken.None).GetAwaiter().GetResult();

// Now you simply use the RSA key to encrypt by setting it in the BlobEncryptionPolicy.
BlobEncryptionPolicy policy = new BlobEncryptionPolicy(rsa, null);
BlobRequestOptions options = new BlobRequestOptions() { EncryptionPolicy = policy };

// Reference a block blob.
CloudBlockBlob blob = contain.GetBlockBlobReference("MyFile.txt");

// Upload using the UploadFromStream method.
using (var stream = System.IO.File.OpenRead(@"C:\Temp\MyFile.txt"))
    blob.UploadFromStream(stream, stream.Length, null, options, null);

【问题讨论】:

  • 您还有其他顾虑吗?如果您没有其他顾虑,请您接受 ii 作为 ana snwer 吗?

标签: c# .net azure-blob-storage azure-keyvault


【解决方案1】:

关于问题,请参考以下步骤。更多详情请参考here

  1. 在 Azure 密钥保管库中创建服务主体并设置访问策略

  2. 代码(安装包``)

 string tenantId = "<sp tenant>";
            string clientId = "<sp appId>";
            string clientSecret = "<sp secret>";
            string connectionString = "";
            ClientSecretCredential cred = new ClientSecretCredential(tenantId, clientId, clientSecret);
            var vaultUri = new Uri("https://jimkey02.vault.azure.net/");
            KeyClient keyClient = new KeyClient(vaultUri, cred);  
            // if you do not have key, please use following code to create
            //KeyVaultKey rasKey = await keyClient.CreateRsaKeyAsync(new CreateRsaKeyOptions("blobKey"));
            KeyVaultKey rasKey = await keyClient.GetKeyAsync("blobKey", "<key version>");
            IKeyEncryptionKey key =new CryptographyClient(rasKey.Id, cred);
            IKeyEncryptionKeyResolver keyResolver = new KeyResolver(cred);
            ClientSideEncryptionOptions encryptionOptions = new ClientSideEncryptionOptions(ClientSideEncryptionVersion.V1_0)
            {
                KeyEncryptionKey = key,
                KeyResolver = keyResolver,
                // string the storage client will use when calling IKeyEncryptionKey.WrapKey()
                KeyWrapAlgorithm = "RSA1_5"
            };

            BlobClientOptions options = new SpecializedBlobClientOptions() { ClientSideEncryption = encryptionOptions };
            BlobClient blob = new BlobServiceClient(connectionString, options).GetBlobContainerClient("test").GetBlobClient("test.txt");
            using (FileStream file = File.OpenRead(@"D:\test.txt"))
            {
                await blob.UploadAsync(file);
            }


            BlobDownloadInfo download = await blob.DownloadAsync();
            using (StreamReader reader = new StreamReader(download.Content)) {
                string text = await reader.ReadToEndAsync();
                Console.WriteLine(text);
            }

【讨论】:

    猜你喜欢
    • 2022-01-27
    • 1970-01-01
    • 2020-06-28
    • 2020-08-22
    • 1970-01-01
    • 2021-03-31
    • 2021-01-04
    • 2021-04-22
    • 2021-10-05
    相关资源
    最近更新 更多