Created
February 4, 2020 07:19
-
-
Save kasunkv/e30aa3cd6bf278756c598dbbb81331dc to your computer and use it in GitHub Desktop.
Using DefaultAzureCredential to run the application locally and in the Cloud
This file contains 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 Azure.Identity; | |
using Microsoft.Extensions.Configuration.AzureAppConfiguration; | |
namespace MusicStore.Web | |
{ | |
public class Program | |
{ | |
... | |
public static IHostBuilder CreateHostBuilder(string[] args) => | |
Host.CreateDefaultBuilder(args) | |
.ConfigureWebHostDefaults(webBuilder => { | |
webBuilder.UseStartup<Startup>(); | |
}) | |
.ConfigureAppConfiguration((context, config) => { | |
var settings = config.Build(); | |
var appConfigEndpoint = settings["AppSettings:AppConfiguration:Endpoint"]; | |
var userAssignedIdentityClientId = settings["AppSettings:Identity:ClientId"]; | |
if (!string.IsNullOrEmpty(appConfigEndpoint)) | |
{ | |
var endpoint = new Uri(appConfigEndpoint); | |
// Create the token credential instance with the client id of the Managed Identity | |
var credentialOptions = new DefaultAzureCredentialOptions { | |
ManagedIdentityClientId = userAssignedIdentityClientId | |
}; | |
config.AddAzureAppConfiguration(options => | |
{ | |
options | |
// Use managed identity to access app configuration | |
.Connect(endpoint, new DefaultAzureCredential(credentialOptions)) | |
... | |
// Configure Azure Key Vault with Managed Identity | |
.ConfigureKeyVault(vaultOpt => | |
{ | |
vaultOpt.SetCredential(new DefaultAzureCredential(credentialOptions)); | |
}) | |
... | |
}); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment