Using Azure Key Vault secrets in MVC app. in .NET Core V8

Richard Scannell 386 Reputation points
2024-11-12T10:48:57+00:00

I’d like to modify the Program.cs for a .NET Core to get a database connection string (DBConnstrVal) from an Azure Key Vault. Currently, this comes from AppSettings.JSON, and is available to the models and controllers in the same ways as the rest of the data in the AppConfig. The app is running locally & will be deployed to a server.

  

This is the sample code for getting the value DBConnstrKey from the Azure Key Vault at keyVaultUri from a console app run locally

var client = new SecretClient(new Uri(keyVaultUri), new DefaultAzureCredential());

var secret = await client.GetSecretAsync(“DBConnstrKey”);

  

The code snippet for the Program.cs below shows how I am loading AppSettings.JSON values & adding role policies. ( Other builder.services statements have been left out for clarity ) .

  

builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme).AddNegotiate();

var Admins = builder.Configuration["AppConfig:Admins"];

var Editors = builder.Configuration["AppConfig:Editors"];

var Readers = builder.Configuration["AppConfig:Readers"];

builder.Services.AddAuthorization(options =>

{

    options.AddPolicy("Admins", policy => policy.RequireRole(Admins));

    options.AddPolicy("Editors", policy => policy.RequireRole(Editors));

    options.AddPolicy("Readers", policy => policy.RequireRole(Readers));

    options.FallbackPolicy = options.DefaultPolicy;

});

 

builder.Services.AddRazorPages();

builder.Services.Configure<AppConfig>(builder.Configuration.GetSection("AppConfig"));

var app = builder.Build();

  

This is the AppConfig class

public class AppConfig

{

    public string AppTitle { get; set; }     

    public string DBConnstrVal { get; set; }

    public string Admins { get; set; }

    public string Editors { get; set; }

    public string Readers { get; set; }

    public string KeyVaultUrl { get; set; }

    public string SecretConnKey { get; set; }

}

How do I modify Program.cs to use the connection string from the Azure Key Vault and make it available to the other models & controllers in the MVC app, like the AppSettings.JSON values are currently  ?

Thanks in advance

( Apologies if posted twice, the previous attempt to post ended up as a 404 )

 

Azure Key Vault
Azure Key Vault
An Azure service that is used to manage and protect cryptographic keys and other secrets used by cloud apps and services.
1,330 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,674 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,111 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 68,236 Reputation points
    2024-11-12T16:14:37.9233333+00:00

    You can supply your own configure provider or use the supplied one.

    https://learn.microsoft.com/en-us/aspnet/core/security/key-vault-configuration?view=aspnetcore-8.0

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.