Skip to content

Instantly share code, notes, and snippets.

@dougwaldron
Created September 3, 2026 14:45
Show Gist options
  • Select an option

  • Save dougwaldron/5bcba033b6dcc715d504001c47b7fc49 to your computer and use it in GitHub Desktop.

Select an option

Save dougwaldron/5bcba033b6dcc715d504001c47b7fc49 to your computer and use it in GitHub Desktop.
Azure Key Vault POC
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<RootNamespace>keyvault_console_app</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.21.0" />
<PackageReference Include="Azure.Security.KeyVault.Secrets" Version="4.11.0" />
</ItemGroup>
</Project>
using Azure;
using Azure.Core;
using Azure.Identity;
using Azure.Security.KeyVault.Secrets;
const string keyVaultName = "DNR-POC-KeyVault-1";
const string keyVaultUri = "https://dnr-poc-keyvault-1.vault.azure.net";
var options = new SecretClientOptions
{
Retry =
{
Delay = TimeSpan.FromSeconds(2),
MaxDelay = TimeSpan.FromSeconds(16),
MaxRetries = 5,
Mode = RetryMode.Exponential,
},
};
var client = new SecretClient(new Uri(keyVaultUri), new DefaultAzureCredential(), options);
const string secretName = "myNewSecret";
Console.WriteLine($"Looking for secret '{secretName}' in {keyVaultName}.");
try
{
KeyVaultSecret secret = await client.GetSecretAsync(secretName);
Console.WriteLine($"Secret '{secret.Name}' is '{secret.Value}'.");
}
catch (RequestFailedException)
{
Console.WriteLine($"Secret '{secretName}' does not exist.");
}
Console.WriteLine();
Console.Write("Input new value for your secret > ");
var secretValue = Console.ReadLine();
Console.Write($"Saving secret '{secretName}' in {keyVaultName} with the value '{secretValue}' ...");
await client.SetSecretAsync(secretName, secretValue);
Console.WriteLine(" done.");
Console.WriteLine();
Console.WriteLine($"Retrieving secret '{secretName}' from {keyVaultName}.");
KeyVaultSecret getSecret = await client.GetSecretAsync(secretName);
Console.WriteLine($"Secret '{getSecret.Name}' is '{getSecret.Value}'.");
// Console.Write("Deleting your secret from " + keyVaultName + " ...");
// await client.StartDeleteSecretAsync(secretName);
// Console.WriteLine(" done.");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment