-
Notifications
You must be signed in to change notification settings - Fork 0
/
HomeController.cs
69 lines (64 loc) · 2.56 KB
/
HomeController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using Azure;
using Azure.Identity;
using Azure.Security.KeyVault.Secrets;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace IdentitySample.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
(bool success, string message, string secret) = GetKeyVaultSecret();
ViewBag.Secret = secret;
ViewBag.Message = message;
ViewBag.KvSuccess = success;
return View();
}
private (bool, string, string) GetKeyVaultSecret()
{
string keyVaultUrl = "";
string secretName = "";
try
{
var _tokenCred = new DefaultAzureCredential();
var kvName = Environment.GetEnvironmentVariable("KeyVaultName");
if(string.IsNullOrWhiteSpace(kvName)) kvName = ConfigurationManager.AppSettings["KeyVault.Name"];
secretName = ConfigurationManager.AppSettings["KeyVault.SecretName"];
keyVaultUrl = $"https://{kvName}.vault.azure.net/";
var _secretClient = new SecretClient(vaultUri: new Uri(keyVaultUrl), credential: _tokenCred);
var resp = _secretClient.GetSecret(secretName);
if (!resp.GetRawResponse().IsError)
{
return (true,"Value pulled directly from Key Vault with 'GetSecret':", resp.Value.Value); ;
}
else
{
return (true,"Identity assigned properly, but failed to Get Secret!!",resp.GetRawResponse().ReasonPhrase);
}
}catch(RequestFailedException rfe)
{
if(rfe.ErrorCode == "Forbidden")
{
return (false,"Unable to get secret from Key Vault!",rfe.Message);
}else if(rfe.ErrorCode == "VaultNotFound")
{
return (false, $"The specified Key Vault {keyVaultUrl} was not found. Please check your environment variables for the 'KeyVaultName' key", rfe.Message);
}
else
{
return (false, "Key Vault request error", rfe.Message);
}
}
catch(Exception exe)
{
return (false,"Something went wrong!",exe.Message);
}
return (false,"Very odd... never really should have gotten here!","");
}
}
}