Skip to content

Instantly share code, notes, and snippets.

@xximjasonxx
Created July 15, 2020 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xximjasonxx/ccfb2f9d7e5687feebc7987d7f8ef57e to your computer and use it in GitHub Desktop.
Save xximjasonxx/ccfb2f9d7e5687feebc7987d7f8ef57e to your computer and use it in GitHub Desktop.
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