var keyVaultClient = new KeyVaultClient(
new KeyVaultClient.AuthenticationCallback(
azureServiceTokenProvider.KeyVaultTokenCallback));
var secret = await keyVaultClient
.GetSecretAsync("https://KeyvaultName.vault.azure.net/secrets/test1")
.ConfigureAwait(false);
ViewData["keyvaultName"] = secret.Value;
//工作正常。但现在我想在一次调用中获取所有秘密并将其绑定到字段
答案 0 :(得分:2)
密钥保管库仅支持一次检索单个秘密值。
答案 1 :(得分:1)
但是这个代码不会在本地运行,即在开发时运行。为此,我们需要安装Azure CLI(azure-cli-2.0.29.msi)以在本地环境中使用MSI。安装此打开的Microsoft azure命令提示符并运行后 " az登录"命令并打开命令提示符中提到的url并复制该url中提示中提到的代码。现在,您可以在本地和应用服务中使用MSI来使用密钥保险库。
Dictionary<string, string> secretlist = new Dictionary<string, string>();
AzureServiceTokenProvider azureServiceTokenProvider = new AzureServiceTokenProvider();
// TO get access token to azureServices
Task<string> accessToken = azureServiceTokenProvider.GetAccessTokenAsync("https://vault.azure.net");
accessToken.Wait();
var keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
var all = keyVaultClient.GetSecretsAsync("https://keyvaultName.vault.azure.net/");
string seperator = "secrets/";
foreach (Microsoft.Azure.KeyVault.Models.SecretItem someItem in all.Result)
{
var secretName = someItem.Identifier;
var secretValue = keyVaultClient.GetSecretAsync(secretName.ToString());
secretValue.Wait();
secretlist.Add(secretName.ToString().Substring(secretName.ToString().IndexOf(seperator) + seperator.Length), secretValue.Result.Value);
}