NILESH
NILESH

Reputation: 55

Azure KeyVault Enable or disable secret key from c#

I have been looking at an option to enable the secret key in the key vault from C# with managed identity. I have full permissions and I am able to create, delete and change the secrets but somehow, if I create a disabled secret key, I cannot read that to re-enable it. Could anyone help to know whether it is possible to enable the disabled key from c#?

Upvotes: 0

Views: 2177

Answers (2)

unknown
unknown

Reputation: 7473

Add my comment as an answer:

You could update the Enabled in SecretProperties of secret, refer to the SDK.

var kvUri = "https://" + keyVaultName + ".vault.azure.net";
var client = new SecretClient(new Uri(kvUri), new DefaultAzureCredential());

KeyVaultSecret secret = client.GetSecret("secret-name");
secret.Properties.Enabled = true;
SecretProperties updatedSecretProperties = client.UpdateSecretProperties(secret.Properties);

Console.WriteLine(updatedSecretProperties.Enabled);

For more details, you could see the official document.

Upvotes: 2

Heath
Heath

Reputation: 3292

Without getting the secret, if you know the name you can simply update it's properties:

var client = new SecretClient(
  new Uri("https://myvault.vault.azure.net"),
  new DefaultAzureCredential());

await client.UpdateSecretPropertiesAsync(
  new SecretProperties("secret-name")
  {
    Enabled = true,
  });

If you already have a KeyVaultSecret, set it's Properties.Enabled to true and pass Properties to the same method above.

Upvotes: 0

Related Questions