How can I resolve 'Value cannot be null. (Parameter 'Version')' error while updating Azure Key Vault's secret expiration date in dotnet code?

huangapple go评论62阅读模式
英文:

How can I resolve 'Value cannot be null. (Parameter 'Version')' error while updating Azure Key Vault's secret expiration date in dotnet code?

问题

I'm trying to update the expiration date of the secret in Azure Key Vault from my .NET code. However, I'm getting an error when calling the method UpdateSecretPropertiesAsync(). Here is some related code:

var credential = new ClientSecretCredential(_configuration[Constants.AZURE_TENANT_ID], _configuration[Constants.AZURE_CLIENT_ID], _configuration[Constants.AZURE_CLIENT_SECRET]);

var client = new SecretClient(new Uri(_configuration[Constants.KEY_VAULT_URL_UAT]), credential);

var secretProperties = new SecretProperties(secretName);
secretProperties.ExpiresOn = DateTimeOffset.Now.AddYears(2);
client.UpdateSecretProperties(secretProperties);  // Here, I'm getting the error (Value cannot be null. (Parameter 'Version'))

This is the documentation related to UpdateSecretPropertiesAsync: link

Can anyone please help me with this as I have very strict timelines?

Even though I tried to add secretProperties.Version, Version is a read-only field. I'm expecting this line of code to work, but for some reason, it is throwing the error. No idea why it is.

英文:

Im trying to update the expiration date of the secret in Azure key vault from my dotnet code.
But Im getting this error on this calling method: UpdateSecretPropertiesAsync().
Here is some related part of code:

var credential = new ClientSecretCredential(_configuration[Constants.AZURE_TENANT_ID], _configuration[Constants.AZURE_CLIENT_ID], _configuration[Constants.AZURE_CLIENT_SECRET]);

var client = new SecretClient(new Uri(_configuration[Constants.KEY_VAULT_URL_UAT]), credential); 

var secretProperties = new SecretProperties(secretName);
secretProperties.ExpiresOn = DateTimeOffset.Now.AddYears(2);
client.UpdateSecretProperties(secretProperties);  // here im getting the error(Value cannot be null. (Parameter 'Version'))

This is the documentation related to UpdateSecretPropertiesAsync: link

Can anyone please help me with this as I have very strict timelines.

Even though I tried to add

secretProperties.Version 

but Version is readonly field.

Im Expecting this line of code to work but for some reason it is throwing the error. No idea why it is.

答案1

得分: 6

更新秘密的属性时,实际上是在更新秘密的特定版本,而不是整个秘密。

因此,不要使用以下方式:

var secretProperties = new SecretProperties(secretName);

尝试获取现有版本:

KeyVaultSecret secretVersion = client.GetSecret(secretName);
var secretProperties = secretVersion.Properties;
英文:

When updating a secret's properties, you're actually updating a specific version of the secret, not the secret as a whole.

So instead of

var secretProperties = new SecretProperties(secretName);

try fetch existing

KeyVaultSecret secretVersion = client.GetSecret(secretName);
var secretProperties = secretVersion.Properties;

huangapple
  • 本文由 发表于 2023年5月25日 16:30:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76330295.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定