In this article, we are going to discuss Azure Key Vault introduction, configuration, and step-by-step implementation using .NET Core 7 Web API.

Agenda

Prerequisites

Introduction

Azure Key Vault Configuration and Implementation using .NET Core 7 Web API
Fig - Key Vault Diagram from Microsoft Documentation

Implementation

Step 1

Create a new .NET Core Web API

Azure Key Vault Configuration and Implementation using .NET Core 7 Web API

Step 2

Configure application

Azure Key Vault Configuration and Implementation using .NET Core 7 Web API

Step 3

Provide additional information

Azure Key Vault Configuration and Implementation using .NET Core 7 Web API

Step 4

Install the following NuGet Package

Azure Key Vault Configuration and Implementation using .NET Core 7 Web API

Step 5

Create some environmental variables inside the app settings JSON file

{
    "Logging": {
        "LogLevel": {
            "Default": "Information",
            "Microsoft.AspNetCore": "Warning"
        }
    },
    "AllowedHosts": "*",
    "KeyVaultConfiguration": {
        "KeyVaultURL": "",
        "ClientId": "",
        "ClientSecret": ""
    }
}

Step 6

Next, register a service inside the Program class

using Microsoft.Extensions.Configuration.AzureKeyVault;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Host.ConfigureAppConfiguration((context, config) => {
    var settings = config.Build();
    var keyVaultURL = settings["KeyVaultConfiguration:KeyVaultURL"];
    var keyVaultClientId = settings["KeyVaultConfiguration:ClientId"];
    var keyVaultClientSecret = settings["KeyVaultConfiguration:ClientSecret"];
    config.AddAzureKeyVault(keyVaultURL, keyVaultClientId, keyVaultClientSecret, new DefaultKeyVaultSecretManager());
});
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) {
    app.UseSwagger();
    app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

Step 7

Create a secrets controller just for demo purposes to access a list of secrets we will create

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace AzureKeyVaultDemo.Controllers {
    [Route("api/[controller]")]
    [ApiController]
    public class SecretsController: ControllerBase {
        private readonly IConfiguration _configuration;
        public SecretsController(IConfiguration configuration) {
                _configuration = configuration;
            }
            [HttpGet]
        public List < string > Get() {
            List < string > result = new List < string > () {
                _configuration["DatabaseConnectionString"],
                    _configuration["RedisCache"]
            };
            return result;
        }
    }
}

Azure Key Vault Setup

Step 1

Open Azure Portal

Step 2

Search Azure Key Vault and click on create

Azure Key Vault Configuration and Implementation using .NET Core 7 Web API

Azure Key Vault Configuration and Implementation using .NET Core 7 Web API

Azure Key Vault Configuration and Implementation using .NET Core 7 Web API

Azure Key Vault Configuration and Implementation using .NET Core 7 Web API

Azure Key Vault Configuration and Implementation using .NET Core 7 Web API

Azure Key Vault Configuration and Implementation using .NET Core 7 Web API

Step 3

Next, add some secrets and their values

Azure Key Vault Configuration and Implementation using .NET Core 7 Web API

Azure Key Vault Configuration and Implementation using .NET Core 7 Web API

Azure Key Vault Configuration and Implementation using .NET Core 7 Web API

Azure Key Vault Configuration and Implementation using .NET Core 7 Web API

Step 4

Search App registration and click on new registrations

Azure Key Vault Configuration and Implementation using .NET Core 7 Web API

Step 5

Provide additional information

Azure Key Vault Configuration and Implementation using .NET Core 7 Web API

Azure Key Vault Configuration and Implementation using .NET Core 7 Web API

Step 6

Click on certificates and secrets

Azure Key Vault Configuration and Implementation using .NET Core 7 Web API

Step 7

Add client secrets and permissions

Azure Key Vault Configuration and Implementation using .NET Core 7 Web API

Azure Key Vault Configuration and Implementation using .NET Core 7 Web API

Note: copy and save the above client secret value (bcV***) because after closing this tab you are not able to see that.

Step 8

Add client secret, key vault URL, and client Id inside app settings JSON file

Step 9

Build and run the application

Step 10

Here we can see Swagger UI, which allows us to access our API endpoints.

Azure Key Vault Configuration and Implementation using .NET Core 7 Web API

Azure Key Vault Configuration and Implementation using .NET Core 7 Web API

Azure Key Vault Configuration and Implementation using .NET Core 7 Web API

GitHub Link

https://github.com/Jaydeep-007/AzureKeyVaultDemo

Conclusion

Here we discussed azure key vault introduction, configuration, and step-by-step implementation using .NET Core Web API.

Happy Coding!