Created
July 15, 2020 00:57
-
-
Save xximjasonxx/ccfb2f9d7e5687feebc7987d7f8ef57e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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