#c# #.net #azure-blob-storage #azure-keyvault
#c# #.net #azure-blob-хранилище #azure-keyvault
Вопрос:
я хочу перенести свой код на SDK v12, но как я могу использовать Azure Keyvault?
Нет класса BlobEncryptionPolicy.
Этот учебник устарел. Он по-прежнему основан на старом SDK.
Код SDK v11:
// 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:TempMyFile.txt"))
blob.UploadFromStream(stream, stream.Length, null, options, null);
Комментарии:
1. У вас есть какие-либо другие проблемы? если у вас нет других проблем, не могли бы вы принять ii как ana snwer?
Ответ №1:
Что касается проблемы, пожалуйста, обратитесь к следующим шагам. Для получения более подробной информации, пожалуйста, обратитесь к here .
-
Создайте участника-службу и задайте политику доступа в хранилище ключей Azure для sp
-
Код (установить пакет `)
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);
}
Комментарии:
1. Вы должны обновить код с помощью ClientSideEncryptionVersion. V2_0 из-за этой уязвимости в системе безопасности: techcommunity.microsoft.com/t5/azure-storage-blog /…