gpt4 book ai didi

c# - 在 Azure Key Vault 上存储长字符串

转载 作者:行者123 更新时间:2023-12-02 06:35:46 25 4
gpt4 key购买 nike

我尝试使用以下代码将配置保存为 Azure Vault 上的 JSON 字符串:

public async Task SetSecretValueAsync(string vault, string keyName, string secretName, string value,
CancellationToken cancellationToken = default)
{
if(string.IsNullOrEmpty(value) || string.IsNullOrEmpty(keyName))
{
return;
}

var cred = new ManagedIdentityCredential(_managedId);

var vaultUri = new Uri($"https://{vault}.vault.azure.net/");
var keyClient = new KeyClient(vaultUri, cred);
var key = (await keyClient.GetKeyAsync(keyName, cancellationToken: cancellationToken).ConfigureAwait(
false)).Value;

var cryptoClient = new CryptographyClient(key.Id, cred);
var valueBytes = Encoding.UTF8.GetBytes(value);
var encryptResult = await cryptoClient.EncryptAsync(EncryptionAlgorithm.RsaOaep, valueBytes, cancellationToken)
.ConfigureAwait(false);
var base64Value = Convert.ToBase64String(encryptResult.Ciphertext);

var client = new SecretClient(vaultUri, cred);
await client.SetSecretAsync(secretName, base64Value, cancellationToken).ConfigureAwait(false);
}

我认为 secret 值的最大长度是 25KB,但是当我尝试超过 255 个字符时,它给了我一个异常(exception)。有没有办法设置长度超过 255 个字符的值?看起来很短。

最佳答案

我认为您遇到的问题与您尝试存储的 secret 的大小无关。最有可能的是 CryptographyClient 调用失败,并且您永远无法调用 SecretClient

var encryptResult = await cryptoClient.EncryptAsync(EncryptionAlgorithm.RsaOaep, valueBytes, cancellationToken)
.ConfigureAwait(false);

我认为 KeyVault 仅支持加密长度最多为 255 个字符的字符串,尽管我找不到确认的来源。这可能是由于所使用的底层加密算法的限制。

关于c# - 在 Azure Key Vault 上存储长字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75044925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com