Created
July 1, 2020 22:46
-
-
Save cmatskas/86aa07728bc3cc7a81d7b75cc1185f37 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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