Skip to content

Instantly share code, notes, and snippets.

@shivanshu3
Created March 2, 2021 23:14
Show Gist options
  • Save shivanshu3/b97bb6c6d395ac830930394a70c636f6 to your computer and use it in GitHub Desktop.
Save shivanshu3/b97bb6c6d395ac830930394a70c636f6 to your computer and use it in GitHub Desktop.
Access Azure KeyVault secrets using service principal client secret in C#
using Microsoft.Azure.KeyVault;
using Microsoft.Azure.Services.AppAuthentication;
using System;
namespace AzureKeyVaultSecrets
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, world!");
var ClientId = "2f354944-0ab0-4e1d-b9e6-74f18f608176";
var TenantId = "7e05f58b-face-41c0-bf88-502e1ee7dcff";
var ClientSecret = "Z9M8e8K..QAZn8y-b__Lj7GxZm8KM4F~rP";
var connectionString = $"RunAs=App;AppId={ClientId};TenantId={TenantId};AppKey={ClientSecret}";
var azureServiceTokenProvider = new AzureServiceTokenProvider(connectionString);
var keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
var secret = keyVaultClient.GetSecretAsync("https://foo.vault.azure.net/", "FooBar").Result.Value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment