Skip to content

Instantly share code, notes, and snippets.

@cmatskas
Created July 1, 2020 22:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmatskas/86aa07728bc3cc7a81d7b75cc1185f37 to your computer and use it in GitHub Desktop.
Save cmatskas/86aa07728bc3cc7a81d7b75cc1185f37 to your computer and use it in GitHub Desktop.
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Azure.KeyVault;
using Microsoft.Azure.Services.AppAuthentication;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.AzureKeyVault;
namespace demo
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, config) =>
{
var builtConfig = config.Build();
var userAssignedId = builtConfig["UserAssignedId"];
AzureServiceTokenProvider azureServiceTokenProvider;
if (string.IsNullOrEmpty(userAssignedId))
{
azureServiceTokenProvider = new AzureServiceTokenProvider();
}
else
{
azureServiceTokenProvider = new AzureServiceTokenProvider($"RunAs=App;AppId={userAssignedId}");
}
var keyVaultClient = new KeyVaultClient(
new KeyVaultClient.AuthenticationCallback(
azureServiceTokenProvider.KeyVaultTokenCallback));
config.AddAzureKeyVault(
$"https://{builtConfig["KeyVaultName"]}.vault.azure.net/",
keyVaultClient,
new DefaultKeyVaultSecretManager());
})
.UseStartup<Startup>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment