Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
using System;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Identity;
using Azure.Security.KeyVault.Secrets;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
namespace KeyVaultTest.Controllers
public class SecretController : ControllerBase
private readonly IConfiguration _configuration;
public SecretController(IConfiguration configuration)
_configuration = configuration;
public async Task<IActionResult> Get(string secretName)
TokenCredential credential = new ClientSecretCredential("<tenant id>", "<application id>", "<application secret>");
if (_configuration["Env"] == "Development")
credential = new ManagedIdentityCredential();
var client = new SecretClient(vaultUri: new Uri("https://<my vault name>"), credential);
var getSecretResponse = await client.GetSecretAsync(secretName);
return Ok(new {
Name = getSecretResponse.Value.Name,
Value = getSecretResponse.Value.Value,
Env = _configuration["Env"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.