skip to Main Content

How do I create and dispose of SecretClient from Azure.Security.KeyVault.Secrets? I’m migrating from KeyVaultClient and can’t seem to find much documentation on this.

2

Answers


  1. Depending on the use case of an application, a SecretClient offers both synchronous and asynchronous activities. You can interact with secrets in Azure Key Vault once a SecretClient has been initialised.

    Creating secret :

    KeyVaultSecret secret = client.SetSecret("secret-name", "secret-value");
    
    Console.WriteLine(secret.Name);
    Console.WriteLine(secret.Value);
    Console.WriteLine(secret.Properties.Version);
    Console.WriteLine(secret.Properties.Enabled);
    

    StartDeleteSecret to delete a secret stored in the Azure Key Vault. When soft-delete is not enabled for the Azure Key Vault permanently disposes the secret.

    DeleteSecretOperation operation = client.StartDeleteSecret("secret-name");
    
    DeletedSecret secret = operation.Value;
    Console.WriteLine(secret.Name);
    Console.WriteLine(secret.Value);
    

    For more information in detail, please refer below links:

    Azure.Security.KeyVault.Secrets

    Azure/azure-sdk-for-net

    Login or Signup to reply.
  2. There is a really good article explaining how Azure SDK’s clients work – Lifetime management for Azure SDK .NET clients:

    • Client lifetime: The main rule of Azure SDK client lifetime management is: treat clients as singletons.
    • Thread-safety: Clients are thread-safe. Models are not thread-safe.
    • Clients are immutable
    • Clients are not disposable: Shared HttpClient as default: One question that comes up often is why aren’t HTTP-based Azure clients implementing IDisposable while internally using an HttpClient that is disposable? All Azure SDK clients, by default, use a single shared HttpClient instance and don’t create any other resources that need to be actively freed. The shared client instance persists throughout the entire application lifetime.

    From Azure Key Vault secret client library for .NET, there are lots of samples on how to use the new clients:

    // Create a new secret client using the default credential from Azure.Identity using environment variables previously set,
    // including AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, and AZURE_TENANT_ID.
    var client = new SecretClient(vaultUri: new Uri(vaultUrl), credential: new DefaultAzureCredential());
    
    // Create a new secret using the secret client.
    KeyVaultSecret secret = client.SetSecret("secret-name", "secret-value");
    
    // Retrieve a secret using the secret client.
    secret = client.GetSecret("secret-name");
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search