Configuring Azure Key Vault in a .NET Core Web API: A Step-by-Step Guide

Waqas Ahmed
3 min readMay 30, 2023

--

Configuring Azure Key Vault
Securing Your .NET Core Web API with Azure Key Vault

Azure Key Vault is a cloud-based service provided by Microsoft Azure that allows you to safeguard and manage cryptographic keys, secrets, and certificates used in your applications and services. It provides a secure storage and management solution for sensitive information, offering centralized control over access to these resources.

Azure Key Vault offers several key features:

  1. Key Management: Azure Key Vault enables you to generate, import, store, and manage cryptographic keys used for encryption, decryption, signing, and verification of data. It supports a variety of key types, including symmetric keys, asymmetric keys, and certificates.
  2. Secret Management: You can securely store and manage secrets, such as passwords, connection strings, API keys, and other sensitive information, in Key Vault. Secrets are stored and accessed programmatically through the Key Vault API, providing a secure and central repository for secret management.
  3. Certificate Management: Key Vault allows you to store and manage X.509 certificates, which are commonly used for secure communication and authentication in applications. You can import or generate certificates in Key Vault, and it provides features like certificate renewal, monitoring, and key rollover.
  4. Access Control and Permissions: Key Vault offers fine-grained access control, allowing you to define access policies and permissions for individuals or applications accessing the keys, secrets, and certificates. This ensures that only authorized users or services can retrieve or use the stored resources.
  5. Auditing and Monitoring: Azure Key Vault integrates with Azure Monitor and Azure Security Center to provide auditing and monitoring capabilities. You can track access to your keys and secrets, monitor usage patterns, and receive alerts on suspicious activities.
  6. Integration with Azure Services: Key Vault seamlessly integrates with other Azure services, such as Azure App Service, Azure Functions, Azure Logic Apps, and Azure Virtual Machines. It provides easy integration and secure access to keys and secrets within your Azure applications.

Azure Key Vault helps you address security concerns associated with storing sensitive information, reducing the risk of accidental exposure or unauthorized access. It enables developers and administrators to manage cryptographic keys and secrets efficiently while adhering to security best practices.

Here’s an example of how you can configure Azure Key Vault in a .NET Core Web API application.

Step 1: Create a new .NET Core Web API project in Visual Studio or using the .NET Core CLI.

Step 2: Install the required NuGet packages:

  • Microsoft.Extensions.Configuration.AzureKeyVault
  • Azure.Extensions.AspNetCore.Configuration.Secrets

Step 3: In your appsettings.json file, add the Azure Key Vault configuration settings:

{
"AzureKeyVault": {
"VaultName": "your-key-vault-name",
"ClientId": "your-client-id",
"ClientSecret": "your-client-secret"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting": "Debug"
}
}
}

Replace your-key-vault-name, your-client-id, and your-client-secret with your actual Azure Key Vault details.

Step 4: Open the Startup.cs file and modify the ConfigureServices method as follows:

using Azure.Extensions.AspNetCore.Configuration.Secrets;
using Azure.Identity;

public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
// Add Azure Key Vault configuration
services.AddAzureClients(builder =>
{
builder.AddSecretClient(new Uri($"https://{Configuration["AzureKeyVault:VaultName"]}.vault.azure.net/"))
.UseCredential(new ClientSecretCredential(
Configuration["AzureKeyVault:ClientId"],
Configuration["AzureKeyVault:ClientSecret"]));
});

// Add other services and configurations...
services.AddControllers();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// Configure other middleware...
app.UseRouting();

// Configure other endpoints...

app.UseAuthorization();

// Configure other middleware...
}
}

Step 5: Use the Key Vault secrets in your controllers or services. For example, you can inject the IConfiguration object into your controller and access the secrets as follows:

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

[ApiController]
[Route("api/[controller]")]
public class SecretsController : ControllerBase
{
private readonly IConfiguration _configuration;

public SecretsController(IConfiguration configuration)
{
_configuration = configuration;
}

[HttpGet]
public ActionResult<string> GetSecret()
{
string secretValue = _configuration["YourSecretKey"];
return Ok(secretValue);
}
}

In this example, replace "YourSecretKey" with the actual key name defined in your Azure Key Vault.

That’s it! With these steps, you’ve configured Azure Key Vault in your .NET Core Web API and can access the secrets securely. Remember to replace the placeholder values with your actual Azure Key Vault details and secret names.

--

--

Waqas Ahmed
Waqas Ahmed

Written by Waqas Ahmed

Microsoft Azure Enthusiast ☁ | Principal Software Engineer | Angular | React.Js | .Net Core | .Net | Azure | Micro Services 👉 https://bit.ly/3AhEyOz

No responses yet