Malawby
Malawby

Reputation: 303

Azure key vault integration from visual studio 2022 connected services

I am trying to store stripe credentials in the Azure Key Vault. I used the connected services tab in my Visual studio application, That seemed to work. However when I run the application locally I get errors in the program file. I am using dotnet 6 core razor pages.

These are the errors hope you can help.

DefaultAzureCredential failed to retrieve a token from the included credentials. See the troubleshooting guide for more information. https://aka.ms/azsdk/net/identity/defaultazurecredential/troubleshoot

This is the Program class ...

public class Program
 {
    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureAppConfiguration((context, config) =>
            {
                var keyVaultEndpoint = new 
  Uri(Environment.GetEnvironmentVariable("VaultUri"));
                config.AddAzureKeyVault(keyVaultEndpoint, new DefaultAzureCredential());

            })
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });

    public static void Main(string[] args)
    {
        var host = CreateHostBuilder(args).Build();
        using (var scope = host.Services.CreateScope())
        {
            var services = scope.ServiceProvider;
            var loggerFactory = services.GetRequiredService<ILoggerFactory>();
            try
            {
                var context = services.GetRequiredService<ApplicationDbContext>();
                var userManager = services.GetRequiredService<UserManager<IdentityUser>>();
                var roleManager = services.GetRequiredService<RoleManager<IdentityRole>>();
            }
            catch (Exception ex)
            {
                var logger = loggerFactory.CreateLogger<Program>();
                logger.LogError(ex, "An error occurred seeding the DB.");
            }
        }
        host.Run();
    }

}

...

Upvotes: 2

Views: 4427

Answers (2)

Vlad Rudenko
Vlad Rudenko

Reputation: 2839

You need just to provide your TenantId for using Visual Studio credential:

new DefaultAzureCredential(new DefaultAzureCredentialOptions { VisualStudioTenantId = "your_tenant_guid" })

PS. The TenantId is visible on the Azure Active Directory main page

Upvotes: 1

Pradeep S J
Pradeep S J

Reputation: 13

Try these

Authenticate you Azure credential in Visual Studio

Tools - Options - Azure Service authentication - Choose an Account or Authenticate with Azure credentials

azure service authentication

Ensure you have right access in AZ Keyvault (Get and List)

Az portal - Keyvault - Access policy - Add - Select principal and save it

Access policy

Validate these settings in launchSettings.json file

launchSettting.json

Upvotes: 1

Related Questions