Skip to content

Instantly share code, notes, and snippets.

Embed
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
{
[ApiController]
[Route("[controller]")]
public class SecretController : ControllerBase
{
private readonly IConfiguration _configuration;
public SecretController(IConfiguration configuration)
{
_configuration = configuration;
}
[HttpGet("{secretName}")]
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>.vault.azure.net/"), 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.